*
* @category string
* @param bool $forcereload shall the singleton be released and new instance created instead?
- * @return string_manager
+ * @return core_string_manager
*/
function get_string_manager($forcereload=false) {
global $CFG;
$translist = explode(',', $CFG->langlist);
}
- if (empty($CFG->langmenucachefile)) {
- $langmenucache = $CFG->cachedir . '/languages';
- } else {
- $langmenucache = $CFG->langmenucachefile;
- }
-
- $singleton = new core_string_manager($CFG->langotherroot, $CFG->langlocalroot,
- !empty($CFG->langstringcache), $translist, $langmenucache);
+ $singleton = new core_string_manager_standard($CFG->langotherroot, $CFG->langlocalroot, $translist);
} else {
- $singleton = new install_string_manager();
+ $singleton = new core_string_manager_install();
}
}
return $singleton;
}
-
-/**
- * Interface for string manager
- *
- * Interface describing class which is responsible for getting
- * of localised strings from language packs.
- *
- * @package core
- * @copyright 2010 Petr Skoda (http://skodak.org)
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-interface string_manager {
- /**
- * Get String returns a requested string
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @param string|object|array $a An object, string or number that can be used
- * within translation strings
- * @param string $lang moodle translation language, null means use current
- * @return string The String !
- */
- public function get_string($identifier, $component = '', $a = null, $lang = null);
-
- /**
- * Does the string actually exist?
- *
- * get_string() is throwing debug warnings, sometimes we do not want them
- * or we want to display better explanation of the problem.
- *
- * Use with care!
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @return boot true if exists
- */
- public function string_exists($identifier, $component);
-
- /**
- * Returns a localised list of all country names, sorted by country keys.
- * @param bool $returnall return all or just enabled
- * @param string $lang moodle translation language, null means use current
- * @return array two-letter country code => translated name.
- */
- public function get_list_of_countries($returnall = false, $lang = null);
-
- /**
- * Returns a localised list of languages, sorted by code keys.
- *
- * @param string $lang moodle translation language, null means use current
- * @param string $standard language list standard
- * iso6392: three-letter language code (ISO 639-2/T) => translated name.
- * @return array language code => translated name
- */
- public function get_list_of_languages($lang = null, $standard = 'iso6392');
-
- /**
- * Checks if the translation exists for the language
- *
- * @param string $lang moodle translation language code
- * @param bool $includeall include also disabled translations
- * @return bool true if exists
- */
- public function translation_exists($lang, $includeall = true);
-
- /**
- * Returns localised list of installed translations
- * @param bool $returnall return all or just enabled
- * @return array moodle translation code => localised translation name
- */
- public function get_list_of_translations($returnall = false);
-
- /**
- * Returns localised list of currencies.
- *
- * @param string $lang moodle translation language, null means use current
- * @return array currency code => localised currency name
- */
- public function get_list_of_currencies($lang = null);
-
- /**
- * Load all strings for one component
- * @param string $component The module the string is associated with
- * @param string $lang
- * @param bool $disablecache Do not use caches, force fetching the strings from sources
- * @param bool $disablelocal Do not use customized strings in xx_local language packs
- * @return array of all string for given component and lang
- */
- public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false);
-
- /**
- * Invalidates all caches, should the implementation use any
- * @param bool $phpunitreset true means called from our PHPUnit integration test reset
- */
- public function reset_caches($phpunitreset = false);
-
- /**
- * Returns string revision counter, this is incremented after any
- * string cache reset.
- * @return int lang string revision counter, -1 if unknown
- */
- public function get_revision();
-}
-
-
-/**
- * Standard string_manager implementation
- *
- * Implements string_manager with getting and printing localised strings
- *
- * @package core
- * @category string
- * @copyright 2010 Petr Skoda (http://skodak.org)
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class core_string_manager implements string_manager {
- /** @var string location of all packs except 'en' */
- protected $otherroot;
- /** @var string location of all lang pack local modifications */
- protected $localroot;
- /** @var cache lang string cache - it will be optimised more later */
- protected $cache;
- /** @var int get_string() counter */
- protected $countgetstring = 0;
- /** @var bool use disk cache */
- protected $usecache;
- /** @var array limit list of translations */
- protected $translist;
- /** @var string location of a file that caches the list of available translations */
- protected $menucache;
-
- /**
- * Create new instance of string manager
- *
- * @param string $otherroot location of downlaoded lang packs - usually $CFG->dataroot/lang
- * @param string $localroot usually the same as $otherroot
- * @param bool $usecache use disk cache
- * @param array $translist limit list of visible translations
- * @param string $menucache the location of a file that caches the list of available translations
- */
- public function __construct($otherroot, $localroot, $usecache, $translist, $menucache) {
- $this->otherroot = $otherroot;
- $this->localroot = $localroot;
- $this->usecache = $usecache;
- $this->translist = $translist;
- $this->menucache = $menucache;
-
- if ($this->usecache) {
- // We can use a proper cache, establish the cache using the 'String cache' definition.
- $this->cache = cache::make('core', 'string');
- } else {
- // We only want a cache for the length of the request, create a static cache.
- $options = array(
- 'simplekeys' => true,
- 'simpledata' => true
- );
- $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options);
- }
- }
-
- /**
- * Returns list of all explicit parent languages for the given language.
- *
- * English (en) is considered as the top implicit parent of all language packs
- * and is not included in the returned list. The language itself is appended to the
- * end of the list. The method is aware of circular dependency risk.
- *
- * @see self::populate_parent_languages()
- * @param string $lang the code of the language
- * @return array all explicit parent languages with the lang itself appended
- */
- public function get_language_dependencies($lang) {
- return $this->populate_parent_languages($lang);
- }
-
- /**
- * Load all strings for one component
- *
- * @param string $component The module the string is associated with
- * @param string $lang
- * @param bool $disablecache Do not use caches, force fetching the strings from sources
- * @param bool $disablelocal Do not use customized strings in xx_local language packs
- * @return array of all string for given component and lang
- */
- public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) {
- global $CFG;
-
- list($plugintype, $pluginname) = core_component::normalize_component($component);
- if ($plugintype == 'core' and is_null($pluginname)) {
- $component = 'core';
- } else {
- $component = $plugintype . '_' . $pluginname;
- }
-
- $cachekey = $lang.'_'.$component;
-
- if (!$disablecache and !$disablelocal) {
- $string = $this->cache->get($cachekey);
- if ($string) {
- return $string;
- }
- }
-
- // No cache found - let us merge all possible sources of the strings.
- if ($plugintype === 'core') {
- $file = $pluginname;
- if ($file === null) {
- $file = 'moodle';
- }
- $string = array();
- // First load english pack.
- if (!file_exists("$CFG->dirroot/lang/en/$file.php")) {
- return array();
- }
- include("$CFG->dirroot/lang/en/$file.php");
- $originalkeys = array_keys($string);
- $originalkeys = array_flip($originalkeys);
-
- // And then corresponding local if present and allowed.
- if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) {
- include("$this->localroot/en_local/$file.php");
- }
- // Now loop through all langs in correct order.
- $deps = $this->get_language_dependencies($lang);
- foreach ($deps as $dep) {
- // The main lang string location.
- if (file_exists("$this->otherroot/$dep/$file.php")) {
- include("$this->otherroot/$dep/$file.php");
- }
- if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) {
- include("$this->localroot/{$dep}_local/$file.php");
- }
- }
-
- } else {
- if (!$location = core_component::get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) {
- return array();
- }
- if ($plugintype === 'mod') {
- // Bloody mod hack.
- $file = $pluginname;
- } else {
- $file = $plugintype . '_' . $pluginname;
- }
- $string = array();
- // First load English pack.
- if (!file_exists("$location/lang/en/$file.php")) {
- // English pack does not exist, so do not try to load anything else.
- return array();
- }
- include("$location/lang/en/$file.php");
- $originalkeys = array_keys($string);
- $originalkeys = array_flip($originalkeys);
- // And then corresponding local english if present.
- if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) {
- include("$this->localroot/en_local/$file.php");
- }
-
- // Now loop through all langs in correct order.
- $deps = $this->get_language_dependencies($lang);
- foreach ($deps as $dep) {
- // Legacy location - used by contrib only.
- if (file_exists("$location/lang/$dep/$file.php")) {
- include("$location/lang/$dep/$file.php");
- }
- // The main lang string location.
- if (file_exists("$this->otherroot/$dep/$file.php")) {
- include("$this->otherroot/$dep/$file.php");
- }
- // Local customisations.
- if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) {
- include("$this->localroot/{$dep}_local/$file.php");
- }
- }
- }
-
- // We do not want any extra strings from other languages - everything must be in en lang pack.
- $string = array_intersect_key($string, $originalkeys);
-
- if (!$disablelocal) {
- // Now we have a list of strings from all possible sources. put it into both in-memory and on-disk
- // caches so we do not need to do all this merging and dependencies resolving again.
- $this->cache->set($cachekey, $string);
- }
- return $string;
- }
-
- /**
- * Does the string actually exist?
- *
- * get_string() is throwing debug warnings, sometimes we do not want them
- * or we want to display better explanation of the problem.
- * Note: Use with care!
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @return boot true if exists
- */
- public function string_exists($identifier, $component) {
- $lang = current_language();
- $string = $this->load_component_strings($component, $lang);
- return isset($string[$identifier]);
- }
-
- /**
- * Get String returns a requested string
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @param string|object|array $a An object, string or number that can be used
- * within translation strings
- * @param string $lang moodle translation language, null means use current
- * @return string The String !
- */
- public function get_string($identifier, $component = '', $a = null, $lang = null) {
- $this->countgetstring++;
- // There are very many uses of these time formating strings without the 'langconfig' component,
- // it would not be reasonable to expect that all of them would be converted during 2.0 migration.
- static $langconfigstrs = array(
- 'strftimedate' => 1,
- 'strftimedatefullshort' => 1,
- 'strftimedateshort' => 1,
- 'strftimedatetime' => 1,
- 'strftimedatetimeshort' => 1,
- 'strftimedaydate' => 1,
- 'strftimedaydatetime' => 1,
- 'strftimedayshort' => 1,
- 'strftimedaytime' => 1,
- 'strftimemonthyear' => 1,
- 'strftimerecent' => 1,
- 'strftimerecentfull' => 1,
- 'strftimetime' => 1);
-
- if (empty($component)) {
- if (isset($langconfigstrs[$identifier])) {
- $component = 'langconfig';
- } else {
- $component = 'moodle';
- }
- }
-
- if ($lang === null) {
- $lang = current_language();
- }
-
- $string = $this->load_component_strings($component, $lang);
-
- if (!isset($string[$identifier])) {
- if ($component === 'pix' or $component === 'core_pix') {
- // This component contains only alt tags for emoticons, not all of them are supposed to be defined.
- return '';
- }
- if ($identifier === 'parentlanguage' and ($component === 'langconfig' or $component === 'core_langconfig')) {
- // Identifier parentlanguage is a special string, undefined means use English if not defined.
- return 'en';
- }
- if ($this->usecache) {
- // Maybe the on-disk cache is dirty - let the last attempt be to find the string in original sources,
- // do NOT write the results to disk cache because it may end up in race conditions see MDL-31904.
- $this->usecache = false;
- $string = $this->load_component_strings($component, $lang, true);
- $this->usecache = true;
- }
- if (!isset($string[$identifier])) {
- // The string is still missing - should be fixed by developer.
- list($plugintype, $pluginname) = core_component::normalize_component($component);
- if ($plugintype == 'core') {
- $file = "lang/en/{$component}.php";
- } else if ($plugintype == 'mod') {
- $file = "mod/{$pluginname}/lang/en/{$pluginname}.php";
- } else {
- $path = core_component::get_plugin_directory($plugintype, $pluginname);
- $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php";
- }
- debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " .
- "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER);
- return "[[$identifier]]";
- }
- }
-
- $string = $string[$identifier];
-
- if ($a !== null) {
- // Process array's and objects (except lang_strings).
- if (is_array($a) or (is_object($a) && !($a instanceof lang_string))) {
- $a = (array)$a;
- $search = array();
- $replace = array();
- foreach ($a as $key => $value) {
- if (is_int($key)) {
- // We do not support numeric keys - sorry!
- continue;
- }
- if (is_array($value) or (is_object($value) && !($value instanceof lang_string))) {
- // We support just string or lang_string as value.
- continue;
- }
- $search[] = '{$a->'.$key.'}';
- $replace[] = (string)$value;
- }
- if ($search) {
- $string = str_replace($search, $replace, $string);
- }
- } else {
- $string = str_replace('{$a}', (string)$a, $string);
- }
- }
-
- return $string;
- }
-
- /**
- * Returns information about the string_manager performance.
- *
- * @return array
- */
- public function get_performance_summary() {
- return array(array(
- 'langcountgetstring' => $this->countgetstring,
- ), array(
- 'langcountgetstring' => 'get_string calls',
- ));
- }
-
- /**
- * Returns a localised list of all country names, sorted by localised name.
- *
- * @param bool $returnall return all or just enabled
- * @param string $lang moodle translation language, null means use current
- * @return array two-letter country code => translated name.
- */
- public function get_list_of_countries($returnall = false, $lang = null) {
- global $CFG;
-
- if ($lang === null) {
- $lang = current_language();
- }
-
- $countries = $this->load_component_strings('core_countries', $lang);
- core_collator::asort($countries);
- if (!$returnall and !empty($CFG->allcountrycodes)) {
- $enabled = explode(',', $CFG->allcountrycodes);
- $return = array();
- foreach ($enabled as $c) {
- if (isset($countries[$c])) {
- $return[$c] = $countries[$c];
- }
- }
- return $return;
- }
-
- return $countries;
- }
-
- /**
- * Returns a localised list of languages, sorted by code keys.
- *
- * @param string $lang moodle translation language, null means use current
- * @param string $standard language list standard
- * - iso6392: three-letter language code (ISO 639-2/T) => translated name
- * - iso6391: two-letter langauge code (ISO 639-1) => translated name
- * @return array language code => translated name
- */
- public function get_list_of_languages($lang = null, $standard = 'iso6391') {
- if ($lang === null) {
- $lang = current_language();
- }
-
- if ($standard === 'iso6392') {
- $langs = $this->load_component_strings('core_iso6392', $lang);
- ksort($langs);
- return $langs;
-
- } else if ($standard === 'iso6391') {
- $langs2 = $this->load_component_strings('core_iso6392', $lang);
- static $mapping = array('aar' => 'aa', 'abk' => 'ab', 'afr' => 'af', 'aka' => 'ak', 'sqi' => 'sq', 'amh' => 'am', 'ara' => 'ar', 'arg' => 'an', 'hye' => 'hy',
- 'asm' => 'as', 'ava' => 'av', 'ave' => 'ae', 'aym' => 'ay', 'aze' => 'az', 'bak' => 'ba', 'bam' => 'bm', 'eus' => 'eu', 'bel' => 'be', 'ben' => 'bn', 'bih' => 'bh',
- 'bis' => 'bi', 'bos' => 'bs', 'bre' => 'br', 'bul' => 'bg', 'mya' => 'my', 'cat' => 'ca', 'cha' => 'ch', 'che' => 'ce', 'zho' => 'zh', 'chu' => 'cu', 'chv' => 'cv',
- 'cor' => 'kw', 'cos' => 'co', 'cre' => 'cr', 'ces' => 'cs', 'dan' => 'da', 'div' => 'dv', 'nld' => 'nl', 'dzo' => 'dz', 'eng' => 'en', 'epo' => 'eo', 'est' => 'et',
- 'ewe' => 'ee', 'fao' => 'fo', 'fij' => 'fj', 'fin' => 'fi', 'fra' => 'fr', 'fry' => 'fy', 'ful' => 'ff', 'kat' => 'ka', 'deu' => 'de', 'gla' => 'gd', 'gle' => 'ga',
- 'glg' => 'gl', 'glv' => 'gv', 'ell' => 'el', 'grn' => 'gn', 'guj' => 'gu', 'hat' => 'ht', 'hau' => 'ha', 'heb' => 'he', 'her' => 'hz', 'hin' => 'hi', 'hmo' => 'ho',
- 'hrv' => 'hr', 'hun' => 'hu', 'ibo' => 'ig', 'isl' => 'is', 'ido' => 'io', 'iii' => 'ii', 'iku' => 'iu', 'ile' => 'ie', 'ina' => 'ia', 'ind' => 'id', 'ipk' => 'ik',
- 'ita' => 'it', 'jav' => 'jv', 'jpn' => 'ja', 'kal' => 'kl', 'kan' => 'kn', 'kas' => 'ks', 'kau' => 'kr', 'kaz' => 'kk', 'khm' => 'km', 'kik' => 'ki', 'kin' => 'rw',
- 'kir' => 'ky', 'kom' => 'kv', 'kon' => 'kg', 'kor' => 'ko', 'kua' => 'kj', 'kur' => 'ku', 'lao' => 'lo', 'lat' => 'la', 'lav' => 'lv', 'lim' => 'li', 'lin' => 'ln',
- 'lit' => 'lt', 'ltz' => 'lb', 'lub' => 'lu', 'lug' => 'lg', 'mkd' => 'mk', 'mah' => 'mh', 'mal' => 'ml', 'mri' => 'mi', 'mar' => 'mr', 'msa' => 'ms', 'mlg' => 'mg',
- 'mlt' => 'mt', 'mon' => 'mn', 'nau' => 'na', 'nav' => 'nv', 'nbl' => 'nr', 'nde' => 'nd', 'ndo' => 'ng', 'nep' => 'ne', 'nno' => 'nn', 'nob' => 'nb', 'nor' => 'no',
- 'nya' => 'ny', 'oci' => 'oc', 'oji' => 'oj', 'ori' => 'or', 'orm' => 'om', 'oss' => 'os', 'pan' => 'pa', 'fas' => 'fa', 'pli' => 'pi', 'pol' => 'pl', 'por' => 'pt',
- 'pus' => 'ps', 'que' => 'qu', 'roh' => 'rm', 'ron' => 'ro', 'run' => 'rn', 'rus' => 'ru', 'sag' => 'sg', 'san' => 'sa', 'sin' => 'si', 'slk' => 'sk', 'slv' => 'sl',
- 'sme' => 'se', 'smo' => 'sm', 'sna' => 'sn', 'snd' => 'sd', 'som' => 'so', 'sot' => 'st', 'spa' => 'es', 'srd' => 'sc', 'srp' => 'sr', 'ssw' => 'ss', 'sun' => 'su',
- 'swa' => 'sw', 'swe' => 'sv', 'tah' => 'ty', 'tam' => 'ta', 'tat' => 'tt', 'tel' => 'te', 'tgk' => 'tg', 'tgl' => 'tl', 'tha' => 'th', 'bod' => 'bo', 'tir' => 'ti',
- 'ton' => 'to', 'tsn' => 'tn', 'tso' => 'ts', 'tuk' => 'tk', 'tur' => 'tr', 'twi' => 'tw', 'uig' => 'ug', 'ukr' => 'uk', 'urd' => 'ur', 'uzb' => 'uz', 'ven' => 've',
- 'vie' => 'vi', 'vol' => 'vo', 'cym' => 'cy', 'wln' => 'wa', 'wol' => 'wo', 'xho' => 'xh', 'yid' => 'yi', 'yor' => 'yo', 'zha' => 'za', 'zul' => 'zu');
- $langs1 = array();
- foreach ($mapping as $c2 => $c1) {
- $langs1[$c1] = $langs2[$c2];
- }
- ksort($langs1);
- return $langs1;
-
- } else {
- debugging('Unsupported $standard parameter in get_list_of_languages() method: '.$standard);
- }
-
- return array();
- }
-
- /**
- * Checks if the translation exists for the language
- *
- * @param string $lang moodle translation language code
- * @param bool $includeall include also disabled translations
- * @return bool true if exists
- */
- public function translation_exists($lang, $includeall = true) {
-
- if (strpos($lang, '_local') !== false) {
- // Local packs are not real translations.
- return false;
- }
- if (!$includeall and !empty($this->translist)) {
- if (!in_array($lang, $this->translist)) {
- return false;
- }
- }
- if ($lang === 'en') {
- // Part of distribution.
- return true;
- }
- return file_exists("$this->otherroot/$lang/langconfig.php");
- }
-
- /**
- * Returns localised list of installed translations
- *
- * @param bool $returnall return all or just enabled
- * @return array moodle translation code => localised translation name
- */
- public function get_list_of_translations($returnall = false) {
- global $CFG;
-
- $languages = array();
-
- if (!empty($CFG->langcache) and is_readable($this->menucache)) {
- // Try to re-use the cached list of all available languages.
- $cachedlist = json_decode(file_get_contents($this->menucache), true);
-
- if (is_array($cachedlist) and !empty($cachedlist)) {
- // The cache file is restored correctly.
-
- if (!$returnall and !empty($this->translist)) {
- // Return just enabled translations.
- foreach ($cachedlist as $langcode => $langname) {
- if (in_array($langcode, $this->translist)) {
- $languages[$langcode] = $langname;
- }
- }
- return $languages;
-
- } else {
- // Return all translations.
- return $cachedlist;
- }
- }
- }
-
- // The cached list of languages is not available, let us populate the list.
- if (!$returnall and !empty($this->translist)) {
- // Return only some translations.
- foreach ($this->translist as $lang) {
- $lang = trim($lang); // Just trim spaces to be a bit more permissive.
- if (strstr($lang, '_local') !== false) {
- continue;
- }
- if (strstr($lang, '_utf8') !== false) {
- continue;
- }
- if ($lang !== 'en' and !file_exists("$this->otherroot/$lang/langconfig.php")) {
- // Some broken or missing lang - can not switch to it anyway.
- continue;
- }
- $string = $this->load_component_strings('langconfig', $lang);
- if (!empty($string['thislanguage'])) {
- $languages[$lang] = $string['thislanguage'].' ('. $lang .')';
- }
- unset($string);
- }
-
- } else {
- // Return all languages available in system.
- $langdirs = get_list_of_plugins('', '', $this->otherroot);
-
- $langdirs = array_merge($langdirs, array("$CFG->dirroot/lang/en" => 'en'));
- // Sort all.
-
- // Loop through all langs and get info.
- foreach ($langdirs as $lang) {
- if (strstr($lang, '_local') !== false) {
- continue;
- }
- if (strstr($lang, '_utf8') !== false) {
- continue;
- }
- $string = $this->load_component_strings('langconfig', $lang);
- if (!empty($string['thislanguage'])) {
- $languages[$lang] = $string['thislanguage'].' ('. $lang .')';
- }
- unset($string);
- }
-
- if (!empty($CFG->langcache) and !empty($this->menucache)) {
- // Cache the list so that it can be used next time.
- core_collator::asort($languages);
- check_dir_exists(dirname($this->menucache), true, true);
- file_put_contents($this->menucache, json_encode($languages));
- @chmod($this->menucache, $CFG->filepermissions);
- }
- }
-
- core_collator::asort($languages);
-
- return $languages;
- }
-
- /**
- * Returns localised list of currencies.
- *
- * @param string $lang moodle translation language, null means use current
- * @return array currency code => localised currency name
- */
- public function get_list_of_currencies($lang = null) {
- if ($lang === null) {
- $lang = current_language();
- }
-
- $currencies = $this->load_component_strings('core_currencies', $lang);
- asort($currencies);
-
- return $currencies;
- }
-
- /**
- * Clears both in-memory and on-disk caches
- * @param bool $phpunitreset true means called from our PHPUnit integration test reset
- */
- public function reset_caches($phpunitreset = false) {
- global $CFG;
- require_once("$CFG->libdir/filelib.php");
-
- // Clear the on-disk disk with aggregated string files.
- $this->cache->purge();
-
- if (!$phpunitreset) {
- // Increment the revision counter.
- $langrev = get_config('core', 'langrev');
- $next = time();
- if ($langrev !== false and $next <= $langrev and $langrev - $next < 60*60) {
- // This resolves problems when reset is requested repeatedly within 1s,
- // the < 1h condition prevents accidental switching to future dates
- // because we might not recover from it.
- $next = $langrev+1;
- }
- set_config('langrev', $next);
- }
-
- // Clear the cache containing the list of available translations
- // and re-populate it again.
- fulldelete($this->menucache);
- $this->get_list_of_translations(true);
-
- // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches.
- if (function_exists('opcache_reset')) {
- opcache_reset();
- }
- }
-
- /**
- * Returns string revision counter, this is incremented after any string cache reset.
- * @return int lang string revision counter, -1 if unknown
- */
- public function get_revision() {
- global $CFG;
- if (isset($CFG->langrev)) {
- return (int)$CFG->langrev;
- } else {
- return -1;
- }
- }
-
- // End of external API.
-
- /**
- * Helper method that recursively loads all parents of the given language.
- *
- * @see self::get_language_dependencies()
- * @param string $lang language code
- * @param array $stack list of parent languages already populated in previous recursive calls
- * @return array list of all parents of the given language with the $lang itself added as the last element
- */
- protected function populate_parent_languages($lang, array $stack = array()) {
-
- // English does not have a parent language.
- if ($lang === 'en') {
- return $stack;
- }
-
- // Prevent circular dependency (and thence the infinitive recursion loop).
- if (in_array($lang, $stack)) {
- return $stack;
- }
-
- // Load language configuration and look for the explicit parent language.
- if (!file_exists("$this->otherroot/$lang/langconfig.php")) {
- return $stack;
- }
- $string = array();
- include("$this->otherroot/$lang/langconfig.php");
-
- if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') {
- unset($string);
- return array_merge(array($lang), $stack);
-
- } else {
- $parentlang = $string['parentlanguage'];
- unset($string);
- return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack));
- }
- }
-}
-
-
-/**
- * Fetches minimum strings for installation
- *
- * Minimalistic string fetching implementation
- * that is used in installer before we fetch the wanted
- * language pack from moodle.org lang download site.
- *
- * @package core
- * @copyright 2010 Petr Skoda (http://skodak.org)
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class install_string_manager implements string_manager {
- /** @var string location of pre-install packs for all langs */
- protected $installroot;
-
- /**
- * Crate new instance of install string manager
- */
- public function __construct() {
- global $CFG;
- $this->installroot = "$CFG->dirroot/install/lang";
- }
-
- /**
- * Load all strings for one component
- * @param string $component The module the string is associated with
- * @param string $lang
- * @param bool $disablecache Do not use caches, force fetching the strings from sources
- * @param bool $disablelocal Do not use customized strings in xx_local language packs
- * @return array of all string for given component and lang
- */
- public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) {
- // Not needed in installer.
- return array();
- }
-
- /**
- * Does the string actually exist?
- *
- * get_string() is throwing debug warnings, sometimes we do not want them
- * or we want to display better explanation of the problem.
- *
- * Use with care!
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @return boot true if exists
- */
- public function string_exists($identifier, $component) {
- // Simple old style hack ;).
- $str = get_string($identifier, $component);
- return (strpos($str, '[[') === false);
- }
-
- /**
- * Get String returns a requested string
- *
- * @param string $identifier The identifier of the string to search for
- * @param string $component The module the string is associated with
- * @param string|object|array $a An object, string or number that can be used
- * within translation strings
- * @param string $lang moodle translation language, null means use current
- * @return string The String !
- */
- public function get_string($identifier, $component = '', $a = null, $lang = null) {
- if (!$component) {
- $component = 'moodle';
- }
-
- if ($lang === null) {
- $lang = current_language();
- }
-
- // Get parent lang.
- $parent = '';
- if ($lang !== 'en' and $identifier !== 'parentlanguage' and $component !== 'langconfig') {
- if (file_exists("$this->installroot/$lang/langconfig.php")) {
- $string = array();
- include("$this->installroot/$lang/langconfig.php");
- if (isset($string['parentlanguage'])) {
- $parent = $string['parentlanguage'];
- }
- unset($string);
- }
- }
-
- // Include en string first.
- if (!file_exists("$this->installroot/en/$component.php")) {
- return "[[$identifier]]";
- }
- $string = array();
- include("$this->installroot/en/$component.php");
-
- // Now override en with parent if defined.
- if ($parent and $parent !== 'en' and file_exists("$this->installroot/$parent/$component.php")) {
- include("$this->installroot/$parent/$component.php");
- }
-
- // Finally override with requested language.
- if ($lang !== 'en' and file_exists("$this->installroot/$lang/$component.php")) {
- include("$this->installroot/$lang/$component.php");
- }
-
- if (!isset($string[$identifier])) {
- return "[[$identifier]]";
- }
-
- $string = $string[$identifier];
-
- if ($a !== null) {
- if (is_object($a) or is_array($a)) {
- $a = (array)$a;
- $search = array();
- $replace = array();
- foreach ($a as $key => $value) {
- if (is_int($key)) {
- // We do not support numeric keys - sorry!
- continue;
- }
- $search[] = '{$a->'.$key.'}';
- $replace[] = (string)$value;
- }
- if ($search) {
- $string = str_replace($search, $replace, $string);
- }
- } else {
- $string = str_replace('{$a}', (string)$a, $string);
- }
- }
-
- return $string;
- }
-
- /**
- * Returns a localised list of all country names, sorted by country keys.
- *
- * @param bool $returnall return all or just enabled
- * @param string $lang moodle translation language, null means use current
- * @return array two-letter country code => translated name.
- */
- public function get_list_of_countries($returnall = false, $lang = null) {
- // Not used in installer.
- return array();
- }
-
- /**
- * Returns a localised list of languages, sorted by code keys.
- *
- * @param string $lang moodle translation language, null means use current
- * @param string $standard language list standard
- * iso6392: three-letter language code (ISO 639-2/T) => translated name.
- * @return array language code => translated name
- */
- public function get_list_of_languages($lang = null, $standard = 'iso6392') {
- // Not used in installer.
- return array();
- }
-
- /**
- * Checks if the translation exists for the language
- *
- * @param string $lang moodle translation language code
- * @param bool $includeall include also disabled translations
- * @return bool true if exists
- */
- public function translation_exists($lang, $includeall = true) {
- return file_exists($this->installroot.'/'.$lang.'/langconfig.php');
- }
-
- /**
- * Returns localised list of installed translations
- * @param bool $returnall return all or just enabled
- * @return array moodle translation code => localised translation name
- */
- public function get_list_of_translations($returnall = false) {
- // Return all is ignored here - we need to know all langs in installer.
- $languages = array();
- // Get raw list of lang directories.
- $langdirs = get_list_of_plugins('install/lang');
- asort($langdirs);
- // Get some info from each lang.
- foreach ($langdirs as $lang) {
- if (file_exists($this->installroot.'/'.$lang.'/langconfig.php')) {
- $string = array();
- include($this->installroot.'/'.$lang.'/langconfig.php');
- if (!empty($string['thislanguage'])) {
- $languages[$lang] = $string['thislanguage'].' ('.$lang.')';
- }
- }
- }
- // Return array.
- return $languages;
- }
-
- /**
- * Returns localised list of currencies.
- *
- * @param string $lang moodle translation language, null means use current
- * @return array currency code => localised currency name
- */
- public function get_list_of_currencies($lang = null) {
- // Not used in installer.
- return array();
- }
-
- /**
- * This implementation does not use any caches.
- *
- * @param bool $phpunitreset true means called from our PHPUnit integration test reset
- */
- public function reset_caches($phpunitreset = false) {
- // Nothing to do.
- }
-
- /**
- * Returns string revision counter, this is incremented after any string cache reset.
- * @return int lang string revision counter, -1 if unknown
- */
- public function get_revision() {
- return -1;
- }
-}
-
-
/**
* Returns a localized string.
*