diff options
Diffstat (limited to 'phpBB/phpbb/language/language.php')
-rw-r--r-- | phpBB/phpbb/language/language.php | 130 |
1 files changed, 106 insertions, 24 deletions
diff --git a/phpBB/phpbb/language/language.php b/phpBB/phpbb/language/language.php index 3298908365..382d4db89e 100644 --- a/phpBB/phpbb/language/language.php +++ b/phpBB/phpbb/language/language.php @@ -83,10 +83,7 @@ class language // Set up default information $this->user_language = false; $this->default_language = false; - $this->lang = array( - // For BC with user::help array - '__help' => array(), - ); + $this->lang = array(); $this->loaded_language_sets = array( 'core' => array(), 'ext' => array(), @@ -112,25 +109,27 @@ class language /** * Function to set user's language to display. * - * @param string $user_lang_iso ISO code of the User's language + * @param string $user_lang_iso ISO code of the User's language + * @param bool $reload Whether or not to reload language files */ - public function set_user_language($user_lang_iso) + public function set_user_language($user_lang_iso, $reload = false) { $this->user_language = $user_lang_iso; - $this->set_fallback_array(); + $this->set_fallback_array($reload); } /** * Function to set the board's default language to display. * * @param string $default_lang_iso ISO code of the board's default language + * @param bool $reload Whether or not to reload language files */ - public function set_default_language($default_lang_iso) + public function set_default_language($default_lang_iso, $reload = false) { $this->default_language = $default_lang_iso; - $this->set_fallback_array(); + $this->set_fallback_array($reload); } /** @@ -155,8 +154,6 @@ class language /** * Add Language Items * - * Note: $use_help is assigned where needed (only use them to force inclusion). - * * Examples: * <code> * $component = array('posting'); @@ -197,6 +194,36 @@ class language } /** + * @param $key array|string The language key we want to know more about. Can be string or array. + * + * @return bool Returns whether the language key is set. + */ + public function is_set($key) + { + // Load common language files if they not loaded yet + if (!$this->common_language_files_loaded) + { + $this->load_common_language_files(); + } + + if (is_array($key)) + { + $lang = &$this->lang[array_shift($key)]; + + foreach ($key as $_key) + { + $lang = &$lang[$_key]; + } + } + else + { + $lang = &$this->lang[$key]; + } + + return isset($lang); + } + + /** * Advanced language substitution * * Function to mimic sprintf() with the possibility of using phpBB's language system to substitute nullar/singular/plural forms. @@ -212,15 +239,28 @@ class language */ public function lang() { + $args = func_get_args(); + $key = array_shift($args); + + return $this->lang_array($key, $args); + } + + /** + * Act like lang() but takes a key and an array of parameters instead of using variadic + * + * @param string|array $key Language key + * @param array $args Parameters + * + * @return array|string + */ + public function lang_array($key, $args = array()) + { // Load common language files if they not loaded yet if (!$this->common_language_files_loaded) { $this->load_common_language_files(); } - $args = func_get_args(); - $key = $args[0]; - if (is_array($key)) { $lang = &$this->lang[array_shift($key)]; @@ -244,26 +284,25 @@ class language // If the language entry is a string, we simply mimic sprintf() behaviour if (is_string($lang)) { - if (sizeof($args) == 1) + if (count($args) === 0) { return $lang; } // Replace key with language entry and simply pass along... - $args[0] = $lang; - return call_user_func_array('sprintf', $args); + return vsprintf($lang, $args); } else if (sizeof($lang) == 0) { // If the language entry is an empty array, we just return the language key - return $args[0]; + return $key; } // It is an array... now handle different nullar/singular/plural forms $key_found = false; // We now get the first number passed and will select the key based upon this number - for ($i = 1, $num_args = sizeof($args); $i < $num_args; $i++) + for ($i = 0, $num_args = sizeof($args); $i < $num_args; $i++) { if (is_int($args[$i]) || is_float($args[$i])) { @@ -310,8 +349,7 @@ class language } // Use the language string we determined and pass it to sprintf() - $args[0] = $lang[$key_found]; - return call_user_func_array('sprintf', $args); + return vsprintf($lang[$key_found], $args); } /** @@ -511,20 +549,32 @@ class language } /** + * Returns the ISO code of the used language + * + * @return string The ISO code of the currently used language + */ + public function get_used_language() + { + return $this->language_fallback[0]; + } + + /** * Returns language fallback data * + * @param bool $reload Whether or not to reload language files + * * @return array */ - protected function set_fallback_array() + protected function set_fallback_array($reload = false) { $fallback_array = array(); - if ($this->user_language !== false) + if ($this->user_language) { $fallback_array[] = $this->user_language; } - if ($this->default_language !== false) + if ($this->default_language) { $fallback_array[] = $this->default_language; } @@ -532,6 +582,11 @@ class language $fallback_array[] = self::FALLBACK_LANGUAGE; $this->language_fallback = $fallback_array; + + if ($reload) + { + $this->reload_language_files(); + } } /** @@ -568,4 +623,31 @@ class language $this->loader->load_extension($extension_name, $component, $this->language_fallback, $this->lang); $this->loaded_language_sets['ext'][$extension_name][$component] = true; } + + /** + * Reload language files + */ + protected function reload_language_files() + { + $loaded_files = $this->loaded_language_sets; + $this->loaded_language_sets = array( + 'core' => array(), + 'ext' => array(), + ); + + // Reload core files + foreach ($loaded_files['core'] as $component => $value) + { + $this->load_core_file($component); + } + + // Reload extension files + foreach ($loaded_files['ext'] as $ext_name => $ext_info) + { + foreach ($ext_info as $ext_component => $value) + { + $this->load_extension($ext_name, $ext_component); + } + } + } } |