Merge pull request #3897 from Nicofuma/ticket/14158

[ticket/14158] Add language::lang_array() to avoid using call_user_func_array
This commit is contained in:
Marc Alexander 2015-09-12 17:33:58 +02:00
commit 3bde202a10

View file

@ -238,6 +238,22 @@ class language
* @return string Return localized string or the language key if the translation is not available * @return string Return localized string or the language key if the translation is not available
*/ */
public function lang() 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 // Load common language files if they not loaded yet
if (!$this->common_language_files_loaded) if (!$this->common_language_files_loaded)
@ -245,9 +261,6 @@ class language
$this->load_common_language_files(); $this->load_common_language_files();
} }
$args = func_get_args();
$key = $args[0];
if (is_array($key)) if (is_array($key))
{ {
$lang = &$this->lang[array_shift($key)]; $lang = &$this->lang[array_shift($key)];
@ -271,26 +284,25 @@ class language
// If the language entry is a string, we simply mimic sprintf() behaviour // If the language entry is a string, we simply mimic sprintf() behaviour
if (is_string($lang)) if (is_string($lang))
{ {
if (sizeof($args) == 1) if (count($args) === 0)
{ {
return $lang; return $lang;
} }
// Replace key with language entry and simply pass along... // Replace key with language entry and simply pass along...
$args[0] = $lang; return vsprintf($lang, $args);
return call_user_func_array('sprintf', $args);
} }
else if (sizeof($lang) == 0) else if (sizeof($lang) == 0)
{ {
// If the language entry is an empty array, we just return the language key // 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 // It is an array... now handle different nullar/singular/plural forms
$key_found = false; $key_found = false;
// We now get the first number passed and will select the key based upon this number // 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])) if (is_int($args[$i]) || is_float($args[$i]))
{ {
@ -337,8 +349,7 @@ class language
} }
// Use the language string we determined and pass it to sprintf() // Use the language string we determined and pass it to sprintf()
$args[0] = $lang[$key_found]; return vsprintf($lang[$key_found], $args);
return call_user_func_array('sprintf', $args);
} }
/** /**