Merge branch 'w33_MDL-41019_m26_langcaches' of https://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2013-08-12 13:29:17 +08:00
commit df8cd014be
13 changed files with 1029 additions and 995 deletions

View File

@ -172,6 +172,9 @@ ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include
require_once($CFG->libdir.'/classes/component.php');
require_once($CFG->libdir.'/classes/text.php');
require_once($CFG->libdir.'/classes/string_manager.php');
require_once($CFG->libdir.'/classes/string_manager_install.php');
require_once($CFG->libdir.'/classes/string_manager_standard.php');
require_once($CFG->libdir.'/installlib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/setuplib.php');

View File

@ -365,25 +365,6 @@ $CFG->admin = 'admin';
// Locking resolves race conditions and is strongly recommended for production servers.
// $CFG->preventfilelocking = false;
//
// If $CFG->langstringcache is enabled (which should always be in production
// environment), Moodle keeps aggregated strings in its own internal format
// optimised for performance. By default, this on-disk cache is created in
// $CFG->cachedir/lang. In cluster environment, you may wish to specify
// an alternative location of this cache so that each web server in the cluster
// uses its own local cache and does not need to access the shared dataroot.
// Make sure that the web server process has write permission to this location
// and that it has permission to remove the folder, too (so that the cache can
// be pruned).
//
// $CFG->langcacheroot = '/var/www/moodle/htdocs/altcache/lang';
//
// If $CFG->langcache is enabled (which should always be in production
// environment), Moodle stores the list of available languages in a cache file.
// By default, the file $CFG->dataroot/languages is used. You may wish to
// specify an alternative location of this cache file.
//
// $CFG->langmenucachefile = '/var/www/moodle/htdocs/altcache/languages';
//
// Site default language can be set via standard administration interface. If you
// want to have initial error messages for eventual database connection problems
// localized too, you have to set your language code here.
@ -488,7 +469,7 @@ $CFG->admin = 'admin';
// Prevent JS caching
// $CFG->jsrev = -1; // NOT FOR PRODUCTION SERVERS!
//
// Prevent core_string_manager on-disk cache
// Prevent core_string_manager application caching
// $CFG->langstringcache = false; // NOT FOR PRODUCTION SERVERS!
//
// When working with production data on test servers, no emails or other messages

View File

@ -198,6 +198,9 @@ if (!empty($memlimit) and $memlimit != -1) {
// Continue with lib loading
require_once($CFG->libdir.'/classes/text.php');
require_once($CFG->libdir.'/classes/string_manager.php');
require_once($CFG->libdir.'/classes/string_manager_install.php');
require_once($CFG->libdir.'/classes/string_manager_standard.php');
require_once($CFG->libdir.'/weblib.php');
require_once($CFG->libdir.'/outputlib.php');
require_once($CFG->libdir.'/dmllib.php');

View File

@ -46,6 +46,7 @@ $string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
$string['cachedef_groupdata'] = 'Course group information';
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
$string['cachedef_langmenu'] = 'List of available languages';
$string['cachedef_locking'] = 'Locking';
$string['cachedef_observers'] = 'Event observers';
$string['cachedef_plugininfo_base'] = 'Plugin info - base';

View File

@ -0,0 +1,129 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* String manager interface.
*
* @package core
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Interface for string manager
*
* Interface describing class which is responsible for getting
* of localised strings from language packs.
*
* @package core
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface core_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 bool 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();
}

View File

@ -0,0 +1,248 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Installation time string manager.
*
* @package core
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* 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 core_string_manager_install implements core_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'];
}
}
}
// 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;
}
}

View File

@ -0,0 +1,622 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Standard string manager.
*
* @package core
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Standard string_manager implementation
*
* Implements string_manager with getting and printing localised strings
*
* @package core
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_string_manager_standard implements core_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 $translist;
/** @var cache stores list of available translations */
protected $menucache;
/**
* Create new instance of string manager
*
* @param string $otherroot location of downloaded lang packs - usually $CFG->dataroot/lang
* @param string $localroot usually the same as $otherroot
* @param array $translist limit list of visible translations
*/
public function __construct($otherroot, $localroot, $translist) {
$this->otherroot = $otherroot;
$this->localroot = $localroot;
if ($translist) {
$this->translist = array_combine($translist, $translist);
} else {
$this->translist = array();
}
if ($this->get_revision() > 0) {
// We can use a proper cache, establish the cache using the 'String cache' definition.
$this->cache = cache::make('core', 'string');
$this->menucache = cache::make('core', 'langmenu');
} 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);
$this->menucache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'langmenu', 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.'_'.$this->get_key_suffix();
$cachedstring = $this->cache->get($cachekey);
if (!$disablecache and !$disablelocal) {
if ($cachedstring !== false) {
return $cachedstring;
}
}
// 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");
$enstring = $string;
// 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");
$enstring = $string;
// 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, $enstring);
if (!$disablelocal) {
// Now we have a list of strings from all possible sources,
// cache it in MUC cache if not already there.
if ($cachedstring === false) {
$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 formatting 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';
}
// Do not rebuild caches here!
// Devs need to learn to purge all caches after any change or disable $CFG->langstringcache.
if (!isset($string[$identifier])) {
// The string is still missing - should be fixed by developer.
if (debugging('', DEBUG_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 core_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 language 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) {
$translations = $this->get_list_of_translations($includeall);
return isset($translations[$lang]);
}
/**
* 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();
$cachekey = 'list_'.$this->get_key_suffix();
$cachedlist = $this->menucache->get($cachekey);
if ($cachedlist !== false) {
// The cache content is invalid.
if ($returnall or empty($this->translist)) {
return $cachedlist;
}
// Return only enabled translations.
foreach ($cachedlist as $langcode => $langname) {
if (isset($this->translist[$langcode])) {
$languages[$langcode] = $langname;
}
}
return $languages;
}
// Get all languages available in system.
$langdirs = get_list_of_plugins('', 'en', $this->otherroot);
$langdirs["$CFG->dirroot/lang/en"] = 'en';
// Loop through all langs and get info.
foreach ($langdirs as $lang) {
if (strrpos($lang, '_local') !== false) {
// Just a local tweak of some other lang pack.
continue;
}
if (strrpos($lang, '_utf8') !== false) {
// Legacy 1.x lang pack.
continue;
}
if ($lang !== clean_param($lang, PARAM_SAFEDIR)) {
// Invalid lang pack name!
continue;
}
$string = $this->load_component_strings('langconfig', $lang);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('. $lang .')';
}
}
core_collator::asort($languages);
// Cache the list so that it can be used next time.
$this->menucache->set($cachekey, $languages);
if ($returnall or empty($this->translist)) {
return $languages;
}
$cachedlist = $languages;
// Return only enabled translations.
$languages = array();
foreach ($cachedlist as $langcode => $langname) {
if (isset($this->translist[$langcode])) {
$languages[$langcode] = $langname;
}
}
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) {
// Clear the on-disk disk with aggregated string files.
$this->cache->purge();
$this->menucache->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);
}
// Lang packs use PHP files in dataroot, it is better to invalidate opcode caches.
if (function_exists('opcache_reset')) {
opcache_reset();
}
}
/**
* Returns cache key suffix, this enables us to store string + lang menu
* caches in local caches on cluster nodes. We can not use prefix because
* it would cause problems when creating subdirs in cache file store.
* @return string
*/
protected function get_key_suffix() {
$rev = $this->get_revision();
if ($rev < 0) {
// Simple keys do not like minus char.
$rev = 0;
}
return $rev;
}
/**
* 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 (empty($CFG->langstringcache)) {
return -1;
}
if (isset($CFG->langrev)) {
return (int)$CFG->langrev;
} else {
return -1;
}
}
/**
* 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') {
return array_merge(array($lang), $stack);
}
$parentlang = $string['parentlanguage'];
return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack));
}
}

View File

@ -29,8 +29,9 @@
$definitions = array(
// Used to store processed lang files.
// The keys used are the component of the string file.
// The keys used are the revision, lang and component of the string file.
// The persistent max size has been based upon student access of the site.
// NOTE: this data may be safely stored in local caches on cluster nodes.
'string' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
@ -39,6 +40,15 @@ $definitions = array(
'persistentmaxsize' => 30
),
// Used to store cache of all available translations.
// NOTE: this data may be safely stored in local caches on cluster nodes.
'langmenu' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
'persistent' => true,
),
// Used to store database meta information.
// The database meta information includes information about tables and there columns.
// Its keys are the table names.

View File

@ -6616,7 +6616,7 @@ function get_parent_language($lang=null) {
*
* @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;
@ -6635,977 +6635,16 @@ function get_string_manager($forcereload=false) {
$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.
*

View File

@ -529,8 +529,7 @@ function get_exception_info($ex) {
// Remove some absolute paths from message and debugging info.
$searches = array();
$replaces = array();
$cfgnames = array('tempdir', 'cachedir', 'localcachedir', 'themedir',
'langmenucachefile', 'langcacheroot', 'dataroot', 'dirroot');
$cfgnames = array('tempdir', 'cachedir', 'localcachedir', 'themedir', 'dataroot', 'dirroot');
foreach ($cfgnames as $cfgname) {
if (property_exists($CFG, $cfgname)) {
$searches[] = $CFG->$cfgname;

View File

@ -34,14 +34,14 @@ require_once($CFG->libdir.'/moodlelib.php');
* @copyright 2013 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_string_manager_testcase extends advanced_testcase {
class core_string_manager_standard_testcase extends advanced_testcase {
public function test_string_manager_instance() {
$this->resetAfterTest();
$otherroot = dirname(__FILE__).'/fixtures/langtest';
$stringman = testable_core_string_manager::instance($otherroot);
$this->assertContains('string_manager', class_implements($stringman));
$this->assertInstanceOf('core_string_manager', $stringman);
}
public function test_get_language_dependencies() {
@ -76,7 +76,7 @@ class core_string_manager_testcase extends advanced_testcase {
* @copyright 2013 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_core_string_manager extends core_string_manager {
class testable_core_string_manager extends core_string_manager_standard {
/**
* Factory method

View File

@ -13,6 +13,8 @@ information provided here is intended especially for developers.
* New $CFG->localcachedir setting useful for cluster nodes. Admins have to update X-Sendfile aliases if used.
* MS SQL Server drivers are now using NVARCHAR(MAX) instead of NTEXT and VARBINARY(MAX) instead of IMAGE,
this change should be fully transparent and it should help significantly with add-on compatibility.
* The string manager classes were renamed. Note that they should not be modified or used directly,
always use get_string_manager() to get instance of the string manager.
DEPRECATIONS:
Various previously deprecated functions have now been altered to throw DEBUG_DEVELOPER debugging notices

View File

@ -3452,11 +3452,8 @@ function get_formatted_help_string($identifier, $component, $ajax = false) {
global $CFG, $OUTPUT;
$sm = get_string_manager();
if (!$sm->string_exists($identifier, $component) ||
!$sm->string_exists($identifier . '_help', $component)) {
// Strings in the on-disk cache may be dirty - try to rebuild it and check again.
$sm->load_component_strings($component, current_language(), true);
}
// Do not rebuild caches here!
// Devs need to learn to purge all caches after any change or disable $CFG->langstringcache.
$data = new stdClass();