Example #1
0
/**
 * Returns all English help files in non-standard location.
 *
 * Searches for lang/en_utf8/help/* files in various types of plugins (blocks, database presets, question types,
 * 3rd party modules etc.) and returns an array of found files details.
 *
 * The English version of the file may be found in
 *  $CFG->dirroot/location/plugin/lang/en_utf8/help/filename
 * The localised version of the found file should be saved into
 *  $CFG->dataroot/lang/currentlang[_local]/help/prefix_plugin/filename (XXX is "prefix" here right?)
 * where "location", "plugin", "prefix" and "filename" are returned as a part of the file record.
 *
 * @return array Array of a file information. Compatible format with {@link lang_standard_locations()}
 */
function lang_help_extra_locations()
{
    global $CFG;
    $files = array();
    $places = places_to_search_for_lang_strings();
    foreach ($places as $prefix => $directories) {
        if ($prefix != '__exceptions') {
            foreach ($directories as $directory) {
                foreach (get_list_of_plugins($directory) as $plugin) {
                    $enlangdirlocation = $CFG->dirroot . '/' . $directory . '/' . $plugin . '/lang/en_utf8/help';
                    foreach (get_directory_list($enlangdirlocation, 'CVS') as $file) {
                        if (substr($file, -5) == '.html' || substr($file, -4) == '.txt') {
                            $fullpath = $enlangdirlocation . '/' . $file;
                            $files[$fullpath] = array('filename' => $file, 'location' => $directory, 'plugin' => $plugin, 'prefix' => $prefix);
                        }
                    }
                }
            }
        }
    }
    return $files;
}
Example #2
0
         }
     }
     $langs = $xlangs;
     unset($xlangs);
 }
 // Define possible locations for help file similar to locations for language strings
 // Note: Always retain module directory as before
 $locations = array();
 if ($module == 'moodle') {
     $locations[$CFG->dataroot . '/lang/'] = $file;
     $locations[$CFG->dirroot . '/lang/'] = $file;
 } else {
     $modfile = $module . '/' . $file;
     $locations[$CFG->dataroot . '/lang/'] = $modfile;
     $locations[$CFG->dirroot . '/lang/'] = $modfile;
     $rules = places_to_search_for_lang_strings();
     $exceptions = $rules['__exceptions'];
     unset($rules['__exceptions']);
     if (!in_array($module, $exceptions)) {
         $dividerpos = strpos($module, '_');
         if ($dividerpos === false) {
             $type = '';
             $plugin = $module;
         } else {
             $type = substr($module, 0, $dividerpos + 1);
             $plugin = substr($module, $dividerpos + 1);
         }
         if (!empty($rules[$type])) {
             foreach ($rules[$type] as $location) {
                 $locations[$CFG->dirroot . "/{$location}/{$plugin}/lang/"] = "{$plugin}/{$file}";
             }
Example #3
0
/**
* Returns a localized string.
*
* Returns the translated string specified by $identifier as
* for $module.  Uses the same format files as STphp.
* $a is an object, string or number that can be used
* within translation strings
*
* eg "hello \$a->firstname \$a->lastname"
* or "hello \$a"
*
* If you would like to directly echo the localized string use
* the function {@link print_string()}
*
* Example usage of this function involves finding the string you would
* like a local equivalent of and using its identifier and module information
* to retrive it.<br/>
* If you open moodle/lang/en/moodle.php and look near line 1031
* you will find a string to prompt a user for their word for student
* <code>
* $string['wordforstudent'] = 'Your word for Student';
* </code>
* So if you want to display the string 'Your word for student'
* in any language that supports it on your site
* you just need to use the identifier 'wordforstudent'
* <code>
* $mystring = '<strong>'. get_string('wordforstudent') .'</strong>';
or
* </code>
* If the string you want is in another file you'd take a slightly
* different approach. Looking in moodle/lang/en/calendar.php you find
* around line 75:
* <code>
* $string['typecourse'] = 'Course event';
* </code>
* If you want to display the string "Course event" in any language
* supported you would use the identifier 'typecourse' and the module 'calendar'
* (because it is in the file calendar.php):
* <code>
* $mystring = '<h1>'. get_string('typecourse', 'calendar') .'</h1>';
* </code>
*
* As a last resort, should the identifier fail to map to a string
* the returned string will be [[ $identifier ]]
*
* @uses $CFG
* @param string $identifier The key identifier for the localized string
* @param string $module The module where the key identifier is stored, usually expressed as the filename in the language pack without the .php on the end but can also be written as mod/forum or grade/export/xls.  If none is specified then moodle.php is used.
* @param mixed $a An object, string or number that can be used
* within translation strings
* @param array $extralocations An array of strings with other locations to look for string files
* @return string The localized string.
*/
function get_string($identifier, $module = '', $a = NULL, $extralocations = NULL)
{
    global $CFG;
    /// originally these special strings were stored in moodle.php now we are only in langconfig.php
    $langconfigstrs = array('alphabet', 'backupnameformat', 'decsep', 'firstdayofweek', 'listsep', 'locale', 'localewin', 'localewincharset', 'oldcharset', 'parentlanguage', 'strftimedate', 'strftimedateshort', 'strftimedatetime', 'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime', 'strftimemonthyear', 'strftimerecent', 'strftimerecentfull', 'strftimetime', 'thischarset', 'thisdirection', 'thislanguage', 'strftimedatetimeshort', 'thousandssep');
    $filetocheck = 'langconfig.php';
    $defaultlang = 'en_utf8';
    if (in_array($identifier, $langconfigstrs)) {
        $module = 'langconfig';
        //This strings are under langconfig.php for 1.6 lang packs
    }
    $lang = current_language();
    if ($module == '') {
        $module = 'moodle';
    }
    /// If the "module" is actually a pathname, then automatically derive the proper module name
    if (strpos($module, '/') !== false) {
        $modulepath = split('/', $module);
        switch ($modulepath[0]) {
            case 'mod':
                $module = $modulepath[1];
                break;
            case 'blocks':
            case 'block':
                $module = 'block_' . $modulepath[1];
                break;
            case 'enrol':
                $module = 'enrol_' . $modulepath[1];
                break;
            case 'format':
                $module = 'format_' . $modulepath[1];
                break;
            case 'grade':
                $module = 'grade' . $modulepath[1] . '_' . $modulepath[2];
                break;
        }
    }
    /// if $a happens to have % in it, double it so sprintf() doesn't break
    if ($a) {
        $a = clean_getstring_data($a);
    }
    /// Define the two or three major locations of language strings for this module
    $locations = array();
    if (!empty($extralocations)) {
        // Calling code has a good idea where to look
        if (is_array($extralocations)) {
            $locations += $extralocations;
        } else {
            if (is_string($extralocations)) {
                $locations[] = $extralocations;
            } else {
                debugging('Bad lang path provided');
            }
        }
    }
    if (isset($CFG->running_installer)) {
        $module = 'installer';
        $filetocheck = 'installer.php';
        $locations[] = $CFG->dirroot . '/install/lang/';
        $locations[] = $CFG->dataroot . '/lang/';
        $locations[] = $CFG->dirroot . '/lang/';
        $defaultlang = 'en_utf8';
    } else {
        $locations[] = $CFG->dataroot . '/lang/';
        $locations[] = $CFG->dirroot . '/lang/';
    }
    /// Add extra places to look for strings for particular plugin types.
    $rules = places_to_search_for_lang_strings();
    $exceptions = $rules['__exceptions'];
    unset($rules['__exceptions']);
    if (!in_array($module, $exceptions)) {
        $dividerpos = strpos($module, '_');
        if ($dividerpos === false) {
            $type = '';
            $plugin = $module;
        } else {
            $type = substr($module, 0, $dividerpos + 1);
            $plugin = substr($module, $dividerpos + 1);
        }
        if ($module == 'local') {
            $locations[] = $CFG->dirroot . '/local/lang/';
        }
        if (!empty($rules[$type])) {
            foreach ($rules[$type] as $location) {
                $locations[] = $CFG->dirroot . "/{$location}/{$plugin}/lang/";
            }
        }
    }
    /// First check all the normal locations for the string in the current language
    $resultstring = '';
    foreach ($locations as $location) {
        $locallangfile = $location . $lang . '_local' . '/' . $module . '.php';
        //first, see if there's a local file
        if (file_exists($locallangfile)) {
            if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) {
                if (eval($result) === FALSE) {
                    trigger_error('Lang error: ' . $identifier . ':' . $locallangfile, E_USER_NOTICE);
                }
                return $resultstring;
            }
        }
        //if local directory not found, or particular string does not exist in local direcotry
        $langfile = $location . $lang . '/' . $module . '.php';
        if (file_exists($langfile)) {
            if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
                if (eval($result) === FALSE) {
                    trigger_error('Lang error: ' . $identifier . ':' . $langfile, E_USER_NOTICE);
                }
                return $resultstring;
            }
        }
    }
    /// If the preferred language was English (utf8) we can abort now
    /// saving some checks beacuse it's the only "root" lang
    if ($lang == 'en_utf8') {
        return '[[' . $identifier . ']]';
    }
    /// Is a parent language defined?  If so, try to find this string in a parent language file
    foreach ($locations as $location) {
        $langfile = $location . $lang . '/' . $filetocheck;
        if (file_exists($langfile)) {
            if ($result = get_string_from_file('parentlanguage', $langfile, "\$parentlang")) {
                if (eval($result) === FALSE) {
                    trigger_error('Lang error: ' . $identifier . ':' . $langfile, E_USER_NOTICE);
                }
                if (!empty($parentlang)) {
                    // found it!
                    //first, see if there's a local file for parent
                    $locallangfile = $location . $parentlang . '_local' . '/' . $module . '.php';
                    if (file_exists($locallangfile)) {
                        if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) {
                            if (eval($result) === FALSE) {
                                trigger_error('Lang error: ' . $identifier . ':' . $locallangfile, E_USER_NOTICE);
                            }
                            return $resultstring;
                        }
                    }
                    //if local directory not found, or particular string does not exist in local direcotry
                    $langfile = $location . $parentlang . '/' . $module . '.php';
                    if (file_exists($langfile)) {
                        if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
                            eval($result);
                            return $resultstring;
                        }
                    }
                }
            }
        }
    }
    /// Our only remaining option is to try English
    foreach ($locations as $location) {
        $locallangfile = $location . $defaultlang . '_local/' . $module . '.php';
        //first, see if there's a local file
        if (file_exists($locallangfile)) {
            if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) {
                eval($result);
                return $resultstring;
            }
        }
        //if local_en not found, or string not found in local_en
        $langfile = $location . $defaultlang . '/' . $module . '.php';
        if (file_exists($langfile)) {
            if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
                eval($result);
                return $resultstring;
            }
        }
    }
    /// And, because under 1.6 en is defined as en_utf8 child, me must try
    /// if it hasn't been queried before.
    if ($defaultlang == 'en') {
        $defaultlang = 'en_utf8';
        foreach ($locations as $location) {
            $locallangfile = $location . $defaultlang . '_local/' . $module . '.php';
            //first, see if there's a local file
            if (file_exists($locallangfile)) {
                if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) {
                    eval($result);
                    return $resultstring;
                }
            }
            //if local_en not found, or string not found in local_en
            $langfile = $location . $defaultlang . '/' . $module . '.php';
            if (file_exists($langfile)) {
                if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) {
                    eval($result);
                    return $resultstring;
                }
            }
        }
    }
    return '[[' . $identifier . ']]';
    // Last resort
}