Skip to content

Commit

Permalink
MDL-15252 language list refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 10, 2010
1 parent 4000129 commit 2357ab4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 129 deletions.
2 changes: 1 addition & 1 deletion admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
//Fist select language
if ($interactive) {
cli_separator();
$languages = install_get_list_of_languages();
$languages = get_list_of_languages();
// format the langs nicely - 3 per line
$c = 0;
$langlist = '';
Expand Down
2 changes: 1 addition & 1 deletion install.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@
get_string('chooselanguagesub', 'install'));
}

$languages = install_get_list_of_languages();
$languages = get_list_of_languages();
echo '<div class="userinput">';
echo '<div class="formrow"><label class="formlabel" for="langselect">'.get_string('language').'</label>';
echo '<select id="langselect" name="lang" class="forminput" onchange="this.form.submit()">';
Expand Down
10 changes: 10 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3468,6 +3468,16 @@ function xmldb_main_upgrade($oldversion) {
set_config('lang', $lang);
}

// tweak langlist
if (!empty($CFG->langlist)) {
$langs = explode(',', $CFG->langlist);
foreach ($langs as $key=>$lang) {
$lang = str_replace('_utf8', '', $lang);
$langs[$key] = $lang;
}
set_config('langlist', implode(',', $langs));
}

// Main savepoint reached
upgrade_main_savepoint($result, 2010040900);
}
Expand Down
30 changes: 0 additions & 30 deletions lib/installlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,6 @@ function install_db_validate($database, $dbhost, $dbuser, $dbpass, $dbname, $pre
}
}

/**
* This function returns a list of languages and their full names. The
* list of available languages is fetched from install/lang/xx/installer.php
* and it's used exclusively by the installation process
*
* @global object
* @return array An associative array with contents in the form of LanguageCode => LanguageName
*/
function install_get_list_of_languages() {
global $CFG;

$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($CFG->dirroot.'/install/lang/'.$lang.'/installer.php')) {
$string = array();
include($CFG->dirroot.'/install/lang/'.$lang.'/installer.php');
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('.$lang.')';
}
}
}
// Return array
return $languages;
}

/**
* Returns content of config.php file.
* @param moodle_database $database database instance
Expand Down
185 changes: 88 additions & 97 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5702,12 +5702,20 @@ public function get_string($identifier, $component = '', $a = NULL);
* @return array two-letter country code => translated name.
*/
public function get_list_of_countries();

/**
* Returns localised list of installed languages
* @param bool $returnall return all or just enabled
*/
public function get_list_of_languages($returnall = false);
}


/**
* Low level string getching implementation.
*
* TODO: implement lang precompilation
*
* @package moodlecore
* @copyright 2010 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down Expand Up @@ -5934,6 +5942,59 @@ public function get_list_of_countries() {
$lang = current_language();
return $this->load_component_strings('countries', $lang);
}

/**
* Returns localised list of installed languages
* @param bool $returnall return all or just enabled
*/
public function get_list_of_languages($returnall = false) {
global $CFG;

$languages = array();

if (!$returnall && !empty($CFG->langlist)) {
// return only languages allowed in langlist admin setting
$langlist = explode(',', $CFG->langlist);
// find existing langs from langlist
foreach ($langlist as $lang) {
$lang = trim($lang); //Just trim spaces to be a bit more permissive
if (strstr($lang, '_local') !== false) {
continue;
}
if ($lang !== 'en' and !file_exists("$this->otherroot/$lang/langconfig.php")) {
// some broken or missing lang - can not swith 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

asort($langdirs);
// Loop through all langs and get info
foreach ($langdirs as $lang) {
if (strstr($lang, '_local') !== false) {
continue;
}
$string = $this->load_component_strings('langconfig', $lang);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('. $lang .')';
}
unset($string);
}
}

return $languages;
}
}


Expand Down Expand Up @@ -6016,6 +6077,30 @@ public function get_string($identifier, $component = '', $a = NULL) {
public function get_list_of_countries() {
return array();
}

/**
* Returns localised list of installed languages
* @param bool $returnall return all or just enabled
*/
public function get_list_of_languages($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.'/installer.php')) {
$string = array();
include($this->installroot.'/'.$lang.'/installer.php');
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('.$lang.')';
}
}
}
// Return array
return $languages;
}
}


Expand Down Expand Up @@ -6159,105 +6244,11 @@ function print_string($identifier, $component = '', $a = NULL) {
*/
function get_list_of_languages($refreshcache=false, $returnall=false) {

global $CFG;

$languages = array();

$filetocheck = 'langconfig.php';

if (!$refreshcache && !$returnall && !empty($CFG->langcache) && file_exists($CFG->dataroot .'/cache/languages')) {
/// read available langs from cache

$lines = file($CFG->dataroot .'/cache/languages');
foreach ($lines as $line) {
$line = trim($line);
if (preg_match('/^(\w+)\s+(.+)/', $line, $matches)) {
$languages[$matches[1]] = $matches[2];
}
}
unset($lines); unset($line); unset($matches);
return $languages;
}

if (!$returnall && !empty($CFG->langlist)) {
/// return only languages allowed in langlist admin setting

$langlist = explode(',', $CFG->langlist);
// find existing langs from langlist
foreach ($langlist as $lang) {
$lang = trim($lang); //Just trim spaces to be a bit more permissive
if (strstr($lang, '_local')!==false) {
continue;
}
/// Search under dirroot/lang
if (file_exists($CFG->dirroot .'/lang/'. $lang .'/'. $filetocheck)) {
include($CFG->dirroot .'/lang/'. $lang .'/'. $filetocheck);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('. $lang .')';
}
unset($string);
}
/// And moodledata/lang
if (file_exists($CFG->dataroot .'/lang/'. $lang .'/'. $filetocheck)) {
include($CFG->dataroot .'/lang/'. $lang .'/'. $filetocheck);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'].' ('. $lang .')';
}
unset($string);
}
}

} else {
/// return all languages available in system
/// Fetch langs from moodle/lang directory
$langdirs = get_list_of_plugins('lang');
/// Fetch langs from moodledata/lang directory
$langdirs2 = get_list_of_plugins('lang', '', $CFG->dataroot);
/// Merge both lists of langs
$langdirs = array_merge($langdirs, $langdirs2);
/// Sort all
asort($langdirs);
/// Get some info from each lang (first from moodledata, then from moodle)
foreach ($langdirs as $lang) {
if (strstr($lang, '_local')!==false) {
continue;
}
/// Search under moodledata/lang
if (file_exists($CFG->dataroot .'/lang/'. $lang .'/'. $filetocheck)) {
include($CFG->dataroot .'/lang/'. $lang .'/'. $filetocheck);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'] .' ('. $lang .')';
}
unset($string);
}
/// And dirroot/lang
if (file_exists($CFG->dirroot .'/lang/'. $lang .'/'. $filetocheck)) {
include($CFG->dirroot .'/lang/'. $lang .'/'. $filetocheck);
if (!empty($string['thislanguage'])) {
$languages[$lang] = $string['thislanguage'] .' ('. $lang .')';
}
unset($string);
}
}
}

if ($refreshcache && !empty($CFG->langcache)) {
if ($returnall) {
// we have a list of all langs only, just delete old cache
@unlink($CFG->dataroot.'/cache/languages');

} else {
// store the list of allowed languages
if ($file = fopen($CFG->dataroot .'/cache/languages', 'w')) {
foreach ($languages as $key => $value) {
fwrite($file, "$key $value\n");
}
fclose($file);
}
}
if ($refreshcache) {
// TODO: reimplement caching?; this may not be necessary if we implement lang pack precompilation in dataroot
}

return $languages;
return get_string_manager()->get_list_of_languages($returnall);
}

/**
Expand Down

0 comments on commit 2357ab4

Please sign in to comment.