From ebe86f345e11852251fd48956e1704297950416f Mon Sep 17 00:00:00 2001 From: marcus Date: Mon, 16 Feb 2009 13:52:47 +0000 Subject: Closes #429: Language loading now no longer loads all possible translations - only english + user's preferred language/site preference git-svn-id: https://code.elgg.org/elgg/trunk@2762 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/languages.php | 84 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 25 deletions(-) (limited to 'engine/lib/languages.php') diff --git a/engine/lib/languages.php b/engine/lib/languages.php index 3b05f6753..d40953ee0 100644 --- a/engine/lib/languages.php +++ b/engine/lib/languages.php @@ -30,7 +30,7 @@ global $CONFIG; if (!isset($CONFIG->translations)) $CONFIG->translations = array(); - + $country_code = strtolower($country_code); $country_code = trim($country_code); if (is_array($language_array) && sizeof($language_array) > 0 && $country_code != "") { @@ -40,6 +40,7 @@ } else { $CONFIG->translations[$country_code] = array_merge($CONFIG->translations[$country_code],$language_array); } + return true; } @@ -55,37 +56,30 @@ { global $CONFIG; - $user = get_loggedin_user(); - - if ((isset($user)) && ($user->language)) - $language = $user->language; - - if ((empty($language)) && (isset($CONFIG->language))) - $language = $CONFIG->language; + $language = get_language(); - if (empty($language)) + if (!$language) $language = 'en'; return $language; } /** - * Gets the current language in use by the system or user + * Gets the current language in use by the system or user. + * + * [Marcus Povey 20090216: Not sure why this func is necessary.] * * @return string The language code (eg "en") */ function get_language() { global $CONFIG; - static $lang; - - if (isset($lang)) return $lang; - - $user = get_loggedin_user(); + + $user = get_loggedin_user(); - if ((empty($language)) && (isset($user)) && ($user->language)) + if ((empty($language)) && ($user) && ($user->language)) $language = $user->language; - + if ((empty($language)) && (isset($CONFIG->language))) $language = $CONFIG->language; @@ -124,21 +118,52 @@ * When given a full path, finds translation files and loads them * * @param string $path Full path + * @param bool $load_all If true all languages are loaded, if false only the current language + en are loaded */ - function register_translations($path) { + function register_translations($path, $load_all = false) { global $CONFIG; + // Make a note of this path just incase we need to register this language later + if(!isset($CONFIG->language_paths)) $CONFIG->language_paths = array(); + $CONFIG->language_paths[] = $path; + + // Get the current language based on site defaults and user preference + $current_language = get_current_language(); + if (isset($CONFIG->debug) && $CONFIG->debug == true) error_log("Translations loaded from : $path"); - + if ($handle = opendir($path)) { while ($language = readdir($handle)) { - if (!in_array($language,array('.','..','.svn','CVS', '.DS_Store', 'Thumbs.db',)) && !is_dir($path . $language)) { - @include($path . $language); - } + + if ( + ((in_array($language, array('en.php', $current_language . '.php'))) && (!is_dir($path . $language))) || + (($load_all) && (strpos($language, '.php')!==false) && (!is_dir($path . $language))) + ) + @include_once($path . $language); + } } else error_log("Missing translation path $path"); + + } + + /** + * Reload all translations from all registered paths. + * + * This is only called by functions which need to know all possible translations, namely the + * statistic gathering ones. + * + * TODO: Better on demand loading based on language_paths array + * + * @return bool + */ + function reload_all_translations() + { + global $CONFIG; + + foreach ($CONFIG->language_paths as $path) + register_translations($path, true); } /** @@ -146,7 +171,10 @@ */ function get_installed_translations() { - global $CONFIG; + global $CONFIG; + + // Ensure that all possible translations are loaded + reload_all_translations(); $installed = array(); @@ -167,7 +195,10 @@ */ function get_language_completeness($language) { - global $CONFIG; + global $CONFIG; + + // Ensure that all possible translations are loaded + reload_all_translations(); $language = sanitise_string($language); @@ -187,7 +218,10 @@ */ function get_missing_language_keys($language) { - global $CONFIG; + global $CONFIG; + + // Ensure that all possible translations are loaded + reload_all_translations(); $missing = array(); -- cgit v1.2.3