1 | <?php |
---|
2 | // This file is part of Moodle - http://moodle.org/ |
---|
3 | // |
---|
4 | // Moodle is free software: you can redistribute it and/or modify |
---|
5 | // it under the terms of the GNU General Public License as published by |
---|
6 | // the Free Software Foundation, either version 3 of the License, or |
---|
7 | // (at your option) any later version. |
---|
8 | // |
---|
9 | // Moodle is distributed in the hope that it will be useful, |
---|
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | // GNU General Public License for more details. |
---|
13 | // |
---|
14 | // You should have received a copy of the GNU General Public License |
---|
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | |
---|
17 | /** |
---|
18 | * Standard string manager. |
---|
19 | * |
---|
20 | * @package core |
---|
21 | * @copyright 2010 Petr Skoda {@link http://skodak.org} |
---|
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
---|
23 | */ |
---|
24 | |
---|
25 | defined('MOODLE_INTERNAL') || die(); |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | * Standard string_manager implementation |
---|
30 | * |
---|
31 | * Implements string_manager with getting and printing localised strings |
---|
32 | * |
---|
33 | * @package core |
---|
34 | * @copyright 2010 Petr Skoda {@link http://skodak.org} |
---|
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
---|
36 | */ |
---|
37 | class core_string_manager_standard implements core_string_manager { |
---|
38 | /** @var string location of all packs except 'en' */ |
---|
39 | protected $otherroot; |
---|
40 | /** @var string location of all lang pack local modifications */ |
---|
41 | protected $localroot; |
---|
42 | /** @var cache lang string cache - it will be optimised more later */ |
---|
43 | protected $cache; |
---|
44 | /** @var int get_string() counter */ |
---|
45 | protected $countgetstring = 0; |
---|
46 | /** @var bool use disk cache */ |
---|
47 | protected $translist; |
---|
48 | /** @var cache stores list of available translations */ |
---|
49 | protected $menucache; |
---|
50 | /** @var array list of cached deprecated strings */ |
---|
51 | protected $cacheddeprecated; |
---|
52 | |
---|
53 | /** |
---|
54 | * Create new instance of string manager |
---|
55 | * |
---|
56 | * @param string $otherroot location of downloaded lang packs - usually $CFG->dataroot/lang |
---|
57 | * @param string $localroot usually the same as $otherroot |
---|
58 | * @param array $translist limit list of visible translations |
---|
59 | */ |
---|
60 | public function __construct($otherroot, $localroot, $translist) { |
---|
61 | $this->otherroot = $otherroot; |
---|
62 | $this->localroot = $localroot; |
---|
63 | if ($translist) { |
---|
64 | $this->translist = array_combine($translist, $translist); |
---|
65 | } else { |
---|
66 | $this->translist = array(); |
---|
67 | } |
---|
68 | |
---|
69 | if ($this->get_revision() > 0) { |
---|
70 | // We can use a proper cache, establish the cache using the 'String cache' definition. |
---|
71 | $this->cache = cache::make('core', 'string'); |
---|
72 | $this->menucache = cache::make('core', 'langmenu'); |
---|
73 | } else { |
---|
74 | // We only want a cache for the length of the request, create a static cache. |
---|
75 | $options = array( |
---|
76 | 'simplekeys' => true, |
---|
77 | 'simpledata' => true |
---|
78 | ); |
---|
79 | $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options); |
---|
80 | $this->menucache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'langmenu', array(), $options); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Returns list of all explicit parent languages for the given language. |
---|
86 | * |
---|
87 | * English (en) is considered as the top implicit parent of all language packs |
---|
88 | * and is not included in the returned list. The language itself is appended to the |
---|
89 | * end of the list. The method is aware of circular dependency risk. |
---|
90 | * |
---|
91 | * @see self::populate_parent_languages() |
---|
92 | * @param string $lang the code of the language |
---|
93 | * @return array all explicit parent languages with the lang itself appended |
---|
94 | */ |
---|
95 | public function get_language_dependencies($lang) { |
---|
96 | return $this->populate_parent_languages($lang); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Load all strings for one component |
---|
101 | * |
---|
102 | * @param string $component The module the string is associated with |
---|
103 | * @param string $lang |
---|
104 | * @param bool $disablecache Do not use caches, force fetching the strings from sources |
---|
105 | * @param bool $disablelocal Do not use customized strings in xx_local language packs |
---|
106 | * @return array of all string for given component and lang |
---|
107 | */ |
---|
108 | public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) { |
---|
109 | global $CFG; |
---|
110 | |
---|
111 | list($plugintype, $pluginname) = core_component::normalize_component($component); |
---|
112 | if ($plugintype === 'core' and is_null($pluginname)) { |
---|
113 | $component = 'core'; |
---|
114 | } else { |
---|
115 | $component = $plugintype . '_' . $pluginname; |
---|
116 | } |
---|
117 | |
---|
118 | $cachekey = $lang.'_'.$component.'_'.$this->get_key_suffix(); |
---|
119 | |
---|
120 | $cachedstring = $this->cache->get($cachekey); |
---|
121 | if (!$disablecache and !$disablelocal) { |
---|
122 | if ($cachedstring !== false) { |
---|
123 | return $cachedstring; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | // No cache found - let us merge all possible sources of the strings. |
---|
128 | if ($plugintype === 'core') { |
---|
129 | $file = $pluginname; |
---|
130 | if ($file === null) { |
---|
131 | $file = 'moodle'; |
---|
132 | } |
---|
133 | $string = array(); |
---|
134 | // First load english pack. |
---|
135 | if (!file_exists("$CFG->dirroot/lang/en/$file.php")) { |
---|
136 | return array(); |
---|
137 | } |
---|
138 | include("$CFG->dirroot/lang/en/$file.php"); |
---|
139 | $enstring = $string; |
---|
140 | |
---|
141 | // And then corresponding local if present and allowed. |
---|
142 | if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { |
---|
143 | include("$this->localroot/en_local/$file.php"); |
---|
144 | } |
---|
145 | // Now loop through all langs in correct order. |
---|
146 | $deps = $this->get_language_dependencies($lang); |
---|
147 | foreach ($deps as $dep) { |
---|
148 | // The main lang string location. |
---|
149 | if (file_exists("$this->otherroot/$dep/$file.php")) { |
---|
150 | include("$this->otherroot/$dep/$file.php"); |
---|
151 | } |
---|
152 | if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { |
---|
153 | include("$this->localroot/{$dep}_local/$file.php"); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | } else { |
---|
158 | if (!$location = core_component::get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) { |
---|
159 | return array(); |
---|
160 | } |
---|
161 | if ($plugintype === 'mod') { |
---|
162 | // Bloody mod hack. |
---|
163 | $file = $pluginname; |
---|
164 | } else { |
---|
165 | $file = $plugintype . '_' . $pluginname; |
---|
166 | } |
---|
167 | $string = array(); |
---|
168 | // First load English pack. |
---|
169 | if (!file_exists("$location/lang/en/$file.php")) { |
---|
170 | // English pack does not exist, so do not try to load anything else. |
---|
171 | return array(); |
---|
172 | } |
---|
173 | include("$location/lang/en/$file.php"); |
---|
174 | $enstring = $string; |
---|
175 | // And then corresponding local english if present. |
---|
176 | if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { |
---|
177 | include("$this->localroot/en_local/$file.php"); |
---|
178 | } |
---|
179 | |
---|
180 | // Now loop through all langs in correct order. |
---|
181 | $deps = $this->get_language_dependencies($lang); |
---|
182 | foreach ($deps as $dep) { |
---|
183 | // Legacy location - used by contrib only. |
---|
184 | if (file_exists("$location/lang/$dep/$file.php")) { |
---|
185 | include("$location/lang/$dep/$file.php"); |
---|
186 | } |
---|
187 | // The main lang string location. |
---|
188 | if (file_exists("$this->otherroot/$dep/$file.php")) { |
---|
189 | include("$this->otherroot/$dep/$file.php"); |
---|
190 | } |
---|
191 | // Local customisations. |
---|
192 | if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { |
---|
193 | include("$this->localroot/{$dep}_local/$file.php"); |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | // We do not want any extra strings from other languages - everything must be in en lang pack. |
---|
199 | $string = array_intersect_key($string, $enstring); |
---|
200 | |
---|
201 | if (!$disablelocal) { |
---|
202 | // Now we have a list of strings from all possible sources, |
---|
203 | // cache it in MUC cache if not already there. |
---|
204 | if ($cachedstring === false) { |
---|
205 | $this->cache->set($cachekey, $string); |
---|
206 | } |
---|
207 | } |
---|
208 | return $string; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Parses all deprecated.txt in all plugins lang locations and returns the list of deprecated strings. |
---|
213 | * |
---|
214 | * Static variable is used for caching, this function is only called in dev environment. |
---|
215 | * |
---|
216 | * @return array of deprecated strings in the same format they appear in deprecated.txt files: "identifier,component" |
---|
217 | * where component is a normalised component (i.e. "core_moodle", "mod_assign", etc.) |
---|
218 | */ |
---|
219 | protected function load_deprecated_strings() { |
---|
220 | global $CFG; |
---|
221 | |
---|
222 | if ($this->cacheddeprecated !== null) { |
---|
223 | return $this->cacheddeprecated; |
---|
224 | } |
---|
225 | |
---|
226 | $this->cacheddeprecated = array(); |
---|
227 | $content = ''; |
---|
228 | $filename = $CFG->dirroot . '/lang/en/deprecated.txt'; |
---|
229 | if (file_exists($filename)) { |
---|
230 | $content .= file_get_contents($filename); |
---|
231 | } |
---|
232 | foreach (core_component::get_plugin_types() as $plugintype => $plugintypedir) { |
---|
233 | foreach (core_component::get_plugin_list($plugintype) as $pluginname => $plugindir) { |
---|
234 | $filename = $plugindir.'/lang/en/deprecated.txt'; |
---|
235 | if (file_exists($filename)) { |
---|
236 | $content .= "\n". file_get_contents($filename); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | $strings = preg_split('/\s*\n\s*/', $content, -1, PREG_SPLIT_NO_EMPTY); |
---|
242 | $this->cacheddeprecated = array_flip($strings); |
---|
243 | |
---|
244 | return $this->cacheddeprecated; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * Has string been deprecated? |
---|
249 | * |
---|
250 | * Usually checked only inside get_string() to display debug warnings. |
---|
251 | * |
---|
252 | * @param string $identifier The identifier of the string to search for |
---|
253 | * @param string $component The module the string is associated with |
---|
254 | * @return bool true if deprecated |
---|
255 | */ |
---|
256 | public function string_deprecated($identifier, $component) { |
---|
257 | $deprecated = $this->load_deprecated_strings(); |
---|
258 | list($plugintype, $pluginname) = core_component::normalize_component($component); |
---|
259 | $normcomponent = $pluginname ? ($plugintype . '_' . $pluginname) : $plugintype; |
---|
260 | return isset($deprecated[$identifier . ',' . $normcomponent]); |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Does the string actually exist? |
---|
265 | * |
---|
266 | * get_string() is throwing debug warnings, sometimes we do not want them |
---|
267 | * or we want to display better explanation of the problem. |
---|
268 | * Note: Use with care! |
---|
269 | * |
---|
270 | * @param string $identifier The identifier of the string to search for |
---|
271 | * @param string $component The module the string is associated with |
---|
272 | * @return boot true if exists |
---|
273 | */ |
---|
274 | public function string_exists($identifier, $component) { |
---|
275 | $lang = current_language(); |
---|
276 | $string = $this->load_component_strings($component, $lang); |
---|
277 | return isset($string[$identifier]); |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Get String returns a requested string |
---|
282 | * |
---|
283 | * @param string $identifier The identifier of the string to search for |
---|
284 | * @param string $component The module the string is associated with |
---|
285 | * @param string|object|array $a An object, string or number that can be used |
---|
286 | * within translation strings |
---|
287 | * @param string $lang moodle translation language, null means use current |
---|
288 | * @return string The String ! |
---|
289 | */ |
---|
290 | public function get_string($identifier, $component = '', $a = null, $lang = null) { |
---|
291 | global $CFG; |
---|
292 | |
---|
293 | $this->countgetstring++; |
---|
294 | // There are very many uses of these time formatting strings without the 'langconfig' component, |
---|
295 | // it would not be reasonable to expect that all of them would be converted during 2.0 migration. |
---|
296 | static $langconfigstrs = array( |
---|
297 | 'strftimedate' => 1, |
---|
298 | 'strftimedatefullshort' => 1, |
---|
299 | 'strftimedateshort' => 1, |
---|
300 | 'strftimedatetime' => 1, |
---|
301 | 'strftimedatetimeshort' => 1, |
---|
302 | 'strftimedaydate' => 1, |
---|
303 | 'strftimedaydatetime' => 1, |
---|
304 | 'strftimedayshort' => 1, |
---|
305 | 'strftimedaytime' => 1, |
---|
306 | 'strftimemonthyear' => 1, |
---|
307 | 'strftimerecent' => 1, |
---|
308 | 'strftimerecentfull' => 1, |
---|
309 | 'strftimetime' => 1); |
---|
310 | |
---|
311 | if (empty($component)) { |
---|
312 | if (isset($langconfigstrs[$identifier])) { |
---|
313 | $component = 'langconfig'; |
---|
314 | } else { |
---|
315 | $component = 'moodle'; |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | if ($lang === null) { |
---|
320 | $lang = current_language(); |
---|
321 | } |
---|
322 | |
---|
323 | $string = $this->load_component_strings($component, $lang); |
---|
324 | |
---|
325 | if (!isset($string[$identifier])) { |
---|
326 | if ($component === 'pix' or $component === 'core_pix') { |
---|
327 | // This component contains only alt tags for emoticons, not all of them are supposed to be defined. |
---|
328 | return ''; |
---|
329 | } |
---|
330 | if ($identifier === 'parentlanguage' and ($component === 'langconfig' or $component === 'core_langconfig')) { |
---|
331 | // Identifier parentlanguage is a special string, undefined means use English if not defined. |
---|
332 | return 'en'; |
---|
333 | } |
---|
334 | // Do not rebuild caches here! |
---|
335 | // Devs need to learn to purge all caches after any change or disable $CFG->langstringcache. |
---|
336 | if (!isset($string[$identifier])) { |
---|
337 | // The string is still missing - should be fixed by developer. |
---|
338 | if ($CFG->debugdeveloper) { |
---|
339 | list($plugintype, $pluginname) = core_component::normalize_component($component); |
---|
340 | if ($plugintype === 'core') { |
---|
341 | $file = "lang/en/{$component}.php"; |
---|
342 | } else if ($plugintype == 'mod') { |
---|
343 | $file = "mod/{$pluginname}/lang/en/{$pluginname}.php"; |
---|
344 | } else { |
---|
345 | $path = core_component::get_plugin_directory($plugintype, $pluginname); |
---|
346 | $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php"; |
---|
347 | } |
---|
348 | debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " . |
---|
349 | "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER); |
---|
350 | } |
---|
351 | return "[[$identifier]]"; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | $string = $string[$identifier]; |
---|
356 | |
---|
357 | if ($a !== null) { |
---|
358 | // Process array's and objects (except lang_strings). |
---|
359 | if (is_array($a) or (is_object($a) && !($a instanceof lang_string))) { |
---|
360 | $a = (array)$a; |
---|
361 | $search = array(); |
---|
362 | $replace = array(); |
---|
363 | foreach ($a as $key => $value) { |
---|
364 | if (is_int($key)) { |
---|
365 | // We do not support numeric keys - sorry! |
---|
366 | continue; |
---|
367 | } |
---|
368 | if (is_array($value) or (is_object($value) && !($value instanceof lang_string))) { |
---|
369 | // We support just string or lang_string as value. |
---|
370 | continue; |
---|
371 | } |
---|
372 | $search[] = '{$a->'.$key.'}'; |
---|
373 | $replace[] = (string)$value; |
---|
374 | } |
---|
375 | if ($search) { |
---|
376 | $string = str_replace($search, $replace, $string); |
---|
377 | } |
---|
378 | } else { |
---|
379 | $string = str_replace('{$a}', (string)$a, $string); |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | if ($CFG->debugdeveloper) { |
---|
384 | // Display a debugging message if sting exists but was deprecated. |
---|
385 | if ($this->string_deprecated($identifier, $component)) { |
---|
386 | list($plugintype, $pluginname) = core_component::normalize_component($component); |
---|
387 | $normcomponent = $pluginname ? ($plugintype . '_' . $pluginname) : $plugintype; |
---|
388 | debugging("String [{$identifier},{$normcomponent}] is deprecated. ". |
---|
389 | 'Either you should no longer be using that string, or the string has been incorrectly deprecated, in which case you should report this as a bug. '. |
---|
390 | 'Please refer to https://docs.moodle.org/dev/String_deprecation', DEBUG_DEVELOPER); |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | return $string; |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Returns information about the core_string_manager performance. |
---|
399 | * |
---|
400 | * @return array |
---|
401 | */ |
---|
402 | public function get_performance_summary() { |
---|
403 | return array(array( |
---|
404 | 'langcountgetstring' => $this->countgetstring, |
---|
405 | ), array( |
---|
406 | 'langcountgetstring' => 'get_string calls', |
---|
407 | )); |
---|
408 | } |
---|
409 | |
---|
410 | /** |
---|
411 | * Returns a localised list of all country names, sorted by localised name. |
---|
412 | * |
---|
413 | * @param bool $returnall return all or just enabled |
---|
414 | * @param string $lang moodle translation language, null means use current |
---|
415 | * @return array two-letter country code => translated name. |
---|
416 | */ |
---|
417 | public function get_list_of_countries($returnall = false, $lang = null) { |
---|
418 | global $CFG; |
---|
419 | |
---|
420 | if ($lang === null) { |
---|
421 | $lang = current_language(); |
---|
422 | } |
---|
423 | |
---|
424 | $countries = $this->load_component_strings('core_countries', $lang); |
---|
425 | core_collator::asort($countries); |
---|
426 | if (!$returnall and !empty($CFG->allcountrycodes)) { |
---|
427 | $enabled = explode(',', $CFG->allcountrycodes); |
---|
428 | $return = array(); |
---|
429 | foreach ($enabled as $c) { |
---|
430 | if (isset($countries[$c])) { |
---|
431 | $return[$c] = $countries[$c]; |
---|
432 | } |
---|
433 | } |
---|
434 | return $return; |
---|
435 | } |
---|
436 | |
---|
437 | return $countries; |
---|
438 | } |
---|
439 | |
---|
440 | /** |
---|
441 | * Returns a localised list of languages, sorted by code keys. |
---|
442 | * |
---|
443 | * @param string $lang moodle translation language, null means use current |
---|
444 | * @param string $standard language list standard |
---|
445 | * - iso6392: three-letter language code (ISO 639-2/T) => translated name |
---|
446 | * - iso6391: two-letter language code (ISO 639-1) => translated name |
---|
447 | * @return array language code => translated name |
---|
448 | */ |
---|
449 | public function get_list_of_languages($lang = null, $standard = 'iso6391') { |
---|
450 | if ($lang === null) { |
---|
451 | $lang = current_language(); |
---|
452 | } |
---|
453 | |
---|
454 | if ($standard === 'iso6392') { |
---|
455 | $langs = $this->load_component_strings('core_iso6392', $lang); |
---|
456 | ksort($langs); |
---|
457 | return $langs; |
---|
458 | |
---|
459 | } else if ($standard === 'iso6391') { |
---|
460 | $langs2 = $this->load_component_strings('core_iso6392', $lang); |
---|
461 | static $mapping = array('aar' => 'aa', 'abk' => 'ab', 'afr' => 'af', 'aka' => 'ak', 'sqi' => 'sq', 'amh' => 'am', 'ara' => 'ar', 'arg' => 'an', 'hye' => 'hy', |
---|
462 | 'asm' => 'as', 'ava' => 'av', 'ave' => 'ae', 'aym' => 'ay', 'aze' => 'az', 'bak' => 'ba', 'bam' => 'bm', 'eus' => 'eu', 'bel' => 'be', 'ben' => 'bn', 'bih' => 'bh', |
---|
463 | 'bis' => 'bi', 'bos' => 'bs', 'bre' => 'br', 'bul' => 'bg', 'mya' => 'my', 'cat' => 'ca', 'cha' => 'ch', 'che' => 'ce', 'zho' => 'zh', 'chu' => 'cu', 'chv' => 'cv', |
---|
464 | 'cor' => 'kw', 'cos' => 'co', 'cre' => 'cr', 'ces' => 'cs', 'dan' => 'da', 'div' => 'dv', 'nld' => 'nl', 'dzo' => 'dz', 'eng' => 'en', 'epo' => 'eo', 'est' => 'et', |
---|
465 | 'ewe' => 'ee', 'fao' => 'fo', 'fij' => 'fj', 'fin' => 'fi', 'fra' => 'fr', 'fry' => 'fy', 'ful' => 'ff', 'kat' => 'ka', 'deu' => 'de', 'gla' => 'gd', 'gle' => 'ga', |
---|
466 | 'glg' => 'gl', 'glv' => 'gv', 'ell' => 'el', 'grn' => 'gn', 'guj' => 'gu', 'hat' => 'ht', 'hau' => 'ha', 'heb' => 'he', 'her' => 'hz', 'hin' => 'hi', 'hmo' => 'ho', |
---|
467 | 'hrv' => 'hr', 'hun' => 'hu', 'ibo' => 'ig', 'isl' => 'is', 'ido' => 'io', 'iii' => 'ii', 'iku' => 'iu', 'ile' => 'ie', 'ina' => 'ia', 'ind' => 'id', 'ipk' => 'ik', |
---|
468 | 'ita' => 'it', 'jav' => 'jv', 'jpn' => 'ja', 'kal' => 'kl', 'kan' => 'kn', 'kas' => 'ks', 'kau' => 'kr', 'kaz' => 'kk', 'khm' => 'km', 'kik' => 'ki', 'kin' => 'rw', |
---|
469 | 'kir' => 'ky', 'kom' => 'kv', 'kon' => 'kg', 'kor' => 'ko', 'kua' => 'kj', 'kur' => 'ku', 'lao' => 'lo', 'lat' => 'la', 'lav' => 'lv', 'lim' => 'li', 'lin' => 'ln', |
---|
470 | 'lit' => 'lt', 'ltz' => 'lb', 'lub' => 'lu', 'lug' => 'lg', 'mkd' => 'mk', 'mah' => 'mh', 'mal' => 'ml', 'mri' => 'mi', 'mar' => 'mr', 'msa' => 'ms', 'mlg' => 'mg', |
---|
471 | 'mlt' => 'mt', 'mon' => 'mn', 'nau' => 'na', 'nav' => 'nv', 'nbl' => 'nr', 'nde' => 'nd', 'ndo' => 'ng', 'nep' => 'ne', 'nno' => 'nn', 'nob' => 'nb', 'nor' => 'no', |
---|
472 | 'nya' => 'ny', 'oci' => 'oc', 'oji' => 'oj', 'ori' => 'or', 'orm' => 'om', 'oss' => 'os', 'pan' => 'pa', 'fas' => 'fa', 'pli' => 'pi', 'pol' => 'pl', 'por' => 'pt', |
---|
473 | 'pus' => 'ps', 'que' => 'qu', 'roh' => 'rm', 'ron' => 'ro', 'run' => 'rn', 'rus' => 'ru', 'sag' => 'sg', 'san' => 'sa', 'sin' => 'si', 'slk' => 'sk', 'slv' => 'sl', |
---|
474 | 'sme' => 'se', 'smo' => 'sm', 'sna' => 'sn', 'snd' => 'sd', 'som' => 'so', 'sot' => 'st', 'spa' => 'es', 'srd' => 'sc', 'srp' => 'sr', 'ssw' => 'ss', 'sun' => 'su', |
---|
475 | 'swa' => 'sw', 'swe' => 'sv', 'tah' => 'ty', 'tam' => 'ta', 'tat' => 'tt', 'tel' => 'te', 'tgk' => 'tg', 'tgl' => 'tl', 'tha' => 'th', 'bod' => 'bo', 'tir' => 'ti', |
---|
476 | 'ton' => 'to', 'tsn' => 'tn', 'tso' => 'ts', 'tuk' => 'tk', 'tur' => 'tr', 'twi' => 'tw', 'uig' => 'ug', 'ukr' => 'uk', 'urd' => 'ur', 'uzb' => 'uz', 'ven' => 've', |
---|
477 | 'vie' => 'vi', 'vol' => 'vo', 'cym' => 'cy', 'wln' => 'wa', 'wol' => 'wo', 'xho' => 'xh', 'yid' => 'yi', 'yor' => 'yo', 'zha' => 'za', 'zul' => 'zu'); |
---|
478 | $langs1 = array(); |
---|
479 | foreach ($mapping as $c2 => $c1) { |
---|
480 | $langs1[$c1] = $langs2[$c2]; |
---|
481 | } |
---|
482 | ksort($langs1); |
---|
483 | return $langs1; |
---|
484 | |
---|
485 | } else { |
---|
486 | debugging('Unsupported $standard parameter in get_list_of_languages() method: '.$standard); |
---|
487 | } |
---|
488 | |
---|
489 | return array(); |
---|
490 | } |
---|
491 | |
---|
492 | /** |
---|
493 | * Checks if the translation exists for the language |
---|
494 | * |
---|
495 | * @param string $lang moodle translation language code |
---|
496 | * @param bool $includeall include also disabled translations |
---|
497 | * @return bool true if exists |
---|
498 | */ |
---|
499 | public function translation_exists($lang, $includeall = true) { |
---|
500 | $translations = $this->get_list_of_translations($includeall); |
---|
501 | return isset($translations[$lang]); |
---|
502 | } |
---|
503 | |
---|
504 | /** |
---|
505 | * Returns localised list of installed translations |
---|
506 | * |
---|
507 | * @param bool $returnall return all or just enabled |
---|
508 | * @return array moodle translation code => localised translation name |
---|
509 | */ |
---|
510 | public function get_list_of_translations($returnall = false) { |
---|
511 | global $CFG; |
---|
512 | |
---|
513 | $languages = array(); |
---|
514 | |
---|
515 | $cachekey = 'list_'.$this->get_key_suffix(); |
---|
516 | $cachedlist = $this->menucache->get($cachekey); |
---|
517 | if ($cachedlist !== false) { |
---|
518 | // The cache content is invalid. |
---|
519 | if ($returnall or empty($this->translist)) { |
---|
520 | return $cachedlist; |
---|
521 | } |
---|
522 | // Return only enabled translations. |
---|
523 | foreach ($cachedlist as $langcode => $langname) { |
---|
524 | if (isset($this->translist[$langcode])) { |
---|
525 | $languages[$langcode] = $langname; |
---|
526 | } |
---|
527 | } |
---|
528 | return $languages; |
---|
529 | } |
---|
530 | |
---|
531 | // Get all languages available in system. |
---|
532 | $langdirs = get_list_of_plugins('', 'en', $this->otherroot); |
---|
533 | $langdirs["$CFG->dirroot/lang/en"] = 'en'; |
---|
534 | |
---|
535 | // We use left to right mark to demark the shortcodes contained in LTR brackets, but we need to do |
---|
536 | // this hacky thing to have the utf8 char until we go php7 minimum and can simply put \u200E in |
---|
537 | // a double quoted string. |
---|
538 | $lrm = json_decode('"\u200E"'); |
---|
539 | |
---|
540 | // Loop through all langs and get info. |
---|
541 | foreach ($langdirs as $lang) { |
---|
542 | if (strrpos($lang, '_local') !== false) { |
---|
543 | // Just a local tweak of some other lang pack. |
---|
544 | continue; |
---|
545 | } |
---|
546 | if (strrpos($lang, '_utf8') !== false) { |
---|
547 | // Legacy 1.x lang pack. |
---|
548 | continue; |
---|
549 | } |
---|
550 | if ($lang !== clean_param($lang, PARAM_SAFEDIR)) { |
---|
551 | // Invalid lang pack name! |
---|
552 | continue; |
---|
553 | } |
---|
554 | $string = $this->load_component_strings('langconfig', $lang); |
---|
555 | if (!empty($string['thislanguage'])) { |
---|
556 | $languages[$lang] = $string['thislanguage'].' '.$lrm.'('. $lang .')'.$lrm; |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | core_collator::asort($languages); |
---|
561 | |
---|
562 | // Cache the list so that it can be used next time. |
---|
563 | $this->menucache->set($cachekey, $languages); |
---|
564 | |
---|
565 | if ($returnall or empty($this->translist)) { |
---|
566 | return $languages; |
---|
567 | } |
---|
568 | |
---|
569 | $cachedlist = $languages; |
---|
570 | |
---|
571 | // Return only enabled translations. |
---|
572 | $languages = array(); |
---|
573 | foreach ($cachedlist as $langcode => $langname) { |
---|
574 | if (isset($this->translist[$langcode])) { |
---|
575 | $languages[$langcode] = $langname; |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | return $languages; |
---|
580 | } |
---|
581 | |
---|
582 | /** |
---|
583 | * Returns localised list of currencies. |
---|
584 | * |
---|
585 | * @param string $lang moodle translation language, null means use current |
---|
586 | * @return array currency code => localised currency name |
---|
587 | */ |
---|
588 | public function get_list_of_currencies($lang = null) { |
---|
589 | if ($lang === null) { |
---|
590 | $lang = current_language(); |
---|
591 | } |
---|
592 | |
---|
593 | $currencies = $this->load_component_strings('core_currencies', $lang); |
---|
594 | asort($currencies); |
---|
595 | |
---|
596 | return $currencies; |
---|
597 | } |
---|
598 | |
---|
599 | /** |
---|
600 | * Clears both in-memory and on-disk caches |
---|
601 | * @param bool $phpunitreset true means called from our PHPUnit integration test reset |
---|
602 | */ |
---|
603 | public function reset_caches($phpunitreset = false) { |
---|
604 | // Clear the on-disk disk with aggregated string files. |
---|
605 | $this->cache->purge(); |
---|
606 | $this->menucache->purge(); |
---|
607 | |
---|
608 | if (!$phpunitreset) { |
---|
609 | // Increment the revision counter. |
---|
610 | $langrev = get_config('core', 'langrev'); |
---|
611 | $next = time(); |
---|
612 | if ($langrev !== false and $next <= $langrev and $langrev - $next < 60*60) { |
---|
613 | // This resolves problems when reset is requested repeatedly within 1s, |
---|
614 | // the < 1h condition prevents accidental switching to future dates |
---|
615 | // because we might not recover from it. |
---|
616 | $next = $langrev+1; |
---|
617 | } |
---|
618 | set_config('langrev', $next); |
---|
619 | } |
---|
620 | |
---|
621 | // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches. |
---|
622 | if (function_exists('opcache_reset')) { |
---|
623 | opcache_reset(); |
---|
624 | } |
---|
625 | } |
---|
626 | |
---|
627 | /** |
---|
628 | * Returns cache key suffix, this enables us to store string + lang menu |
---|
629 | * caches in local caches on cluster nodes. We can not use prefix because |
---|
630 | * it would cause problems when creating subdirs in cache file store. |
---|
631 | * @return string |
---|
632 | */ |
---|
633 | protected function get_key_suffix() { |
---|
634 | $rev = $this->get_revision(); |
---|
635 | if ($rev < 0) { |
---|
636 | // Simple keys do not like minus char. |
---|
637 | $rev = 0; |
---|
638 | } |
---|
639 | |
---|
640 | return $rev; |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | * Returns string revision counter, this is incremented after any string cache reset. |
---|
645 | * @return int lang string revision counter, -1 if unknown |
---|
646 | */ |
---|
647 | public function get_revision() { |
---|
648 | global $CFG; |
---|
649 | if (empty($CFG->langstringcache)) { |
---|
650 | return -1; |
---|
651 | } |
---|
652 | if (isset($CFG->langrev)) { |
---|
653 | return (int)$CFG->langrev; |
---|
654 | } else { |
---|
655 | return -1; |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | /** |
---|
660 | * Helper method that recursively loads all parents of the given language. |
---|
661 | * |
---|
662 | * @see self::get_language_dependencies() |
---|
663 | * @param string $lang language code |
---|
664 | * @param array $stack list of parent languages already populated in previous recursive calls |
---|
665 | * @return array list of all parents of the given language with the $lang itself added as the last element |
---|
666 | */ |
---|
667 | protected function populate_parent_languages($lang, array $stack = array()) { |
---|
668 | |
---|
669 | // English does not have a parent language. |
---|
670 | if ($lang === 'en') { |
---|
671 | return $stack; |
---|
672 | } |
---|
673 | |
---|
674 | // Prevent circular dependency (and thence the infinitive recursion loop). |
---|
675 | if (in_array($lang, $stack)) { |
---|
676 | return $stack; |
---|
677 | } |
---|
678 | |
---|
679 | // Load language configuration and look for the explicit parent language. |
---|
680 | if (!file_exists("$this->otherroot/$lang/langconfig.php")) { |
---|
681 | return $stack; |
---|
682 | } |
---|
683 | $string = array(); |
---|
684 | include("$this->otherroot/$lang/langconfig.php"); |
---|
685 | |
---|
686 | if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') { |
---|
687 | return array_merge(array($lang), $stack); |
---|
688 | |
---|
689 | } |
---|
690 | |
---|
691 | $parentlang = $string['parentlanguage']; |
---|
692 | return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack)); |
---|
693 | } |
---|
694 | } |
---|