Skip to content

Commit

Permalink
MDL-22015 new translation_exists() method - improved string_manager e…
Browse files Browse the repository at this point in the history
…ncapsulation
  • Loading branch information
skodak committed Apr 14, 2010
1 parent c11575d commit ef686eb
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions lib/moodlelib.php
Expand Up @@ -755,11 +755,10 @@ function clean_param($param, $type) {

case PARAM_LANG:
$param = clean_param($param, PARAM_SAFEDIR);
$langs = get_string_manager()->get_list_of_translations(true);
if (in_array($param, $langs)) {
if (get_string_manager()->translation_exists($param)) {
return $param;
} else {
return ''; // Specified language is not installed
return ''; // Specified language is not installed or param malformed
}

case PARAM_THEME:
Expand Down Expand Up @@ -3296,8 +3295,7 @@ function create_user_record($username, $password, $auth='manual') {
// fix for MDL-8480
// user CFG lang for user if $newuser->lang is empty
// or $user->lang is not an installed language
$sitelangs = get_string_manager()->get_list_of_translations();
if (empty($newuser->lang) || !isset($sitelangs[$newuser->lang])) {
if (empty($newuser->lang) || !get_string_manager()->translation_exists($newuser->lang)) {
$newuser->lang = $CFG->lang;
}
$newuser->confirmed = 1;
Expand Down Expand Up @@ -5734,6 +5732,15 @@ public function get_list_of_countries($returnall = false, $lang = NULL);
*/
public function get_list_of_languages($lang = NULL, $standard = 'iso6392');

/**
* Does the translation exist?
*
* @param string $lang moodle translation language code
* @param bool include also disabled translations?
* @return boot true if exists
*/
public function translation_exists($lang, $includeall = true);

/**
* Returns localised list of installed translations
* @param bool $returnall return all or just enabled
Expand Down Expand Up @@ -6055,6 +6062,33 @@ public function get_list_of_languages($lang = NULL, $standard = 'iso6392') {
return array();
}

/**
* Does the translation exist?
*
* @param string $lang moodle translation language code
* @param bool include also disabled translations?
* @return boot true if exists
*/
public function translation_exists($lang, $includeall = true) {
global $CFG;

if (strpos($lang, '_local') !== false) {
// _local packs are not real translations
return false;
}
if (!$includeall and !empty($CFG->langlist)) {
$enabled = explode(',', $CFG->langlist);
if (!in_array($lang, $enabled)) {
return false;
}
}
if ($lang === 'en') {
// part of distribution
return true;
}
return file_exists("$this->otherroot/$lang/longconfig.php");
}

/**
* Returns localised list of installed translations
* @param bool $returnall return all or just enabled
Expand Down Expand Up @@ -6270,6 +6304,17 @@ public function get_list_of_languages($lang = NULL, $standard = 'iso6392') {
return array();
}

/**
* Does the translation exist?
*
* @param string $lang moodle translation language code
* @param bool include also disabled translations?
* @return boot 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
Expand Down Expand Up @@ -9173,15 +9218,10 @@ function setup_lang_from_browser() {
}
krsort($langs, SORT_NUMERIC);

$langlist = get_string_manager()->get_list_of_translations();

/// Look for such langs under standard locations
foreach ($langs as $lang) {
$lang = strtolower(clean_param($lang, PARAM_SAFEDIR)); // clean it properly for include
if (!array_key_exists($lang, $langlist)) {
continue; // language not allowed, try next one
}
if (file_exists($CFG->dataroot .'/lang/'. $lang) or file_exists($CFG->dirroot .'/lang/'. $lang)) {
if (get_string_manager()->translation_exists($lang, false)) {
$SESSION->lang = $lang; /// Lang exists, set it in session
break; /// We have finished. Go out
}
Expand Down

0 comments on commit ef686eb

Please sign in to comment.