nullar/singular/plural substitution support. At the moment only the added language entry supports this... we may change other language entries later to support this new "approach". Idea from SHS` and Ashe originally.

More to come... (yes, 3.0.x branch, no mistake)

git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@8800 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2008-08-31 21:47:26 +00:00
parent 5903066d45
commit 54af1cb64a
2 changed files with 73 additions and 0 deletions

View file

@ -1791,6 +1791,72 @@ class user extends session
return; return;
} }
/**
* More advanced language substitution
* Function to mimic sprintf() with the possibility of using phpBB's language system to substitute nullar/singular/plural forms.
* Params are the language key and the parameters to be substituted.
* This function/functionality is inspired by SHS` and Ashe.
*
* Example call: <samp>$user->lang('NUM_POSTS_IN_QUEUE', 1);</samp>
*/
function lang()
{
$args = func_get_args();
$key = $args[0];
// Return if language string does not exist
if (!isset($this->lang[$key]) || (!is_string($this->lang[$key]) && !is_array($this->lang[$key])))
{
return $key;
}
// If the language entry is a string, we simply mimic sprintf() behaviour
if (is_string($this->lang[$key]))
{
if (sizeof($args) == 1)
{
return $this->lang[$key];
}
// Replace key with language entry and simply pass along...
$args[0] = $this->lang[$key];
return call_user_func_array('sprintf', $args);
}
// 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++)
{
if (is_int($args[$i]))
{
$numbers = array_keys($this->lang[$key]);
foreach ($numbers as $num)
{
if ($num > $args[$i])
{
break;
}
$key_found = $num;
}
}
}
// Ok, let's check if the key was found, else use the last entry (because it is mostly the plural form)
if ($key_found === false)
{
$numbers = array_keys($this->lang[$key]);
$key_found = end($numbers);
}
// Use the language string we determined and pass it to sprintf()
$args[0] = $this->lang[$key][$key_found];
return call_user_func_array('sprintf', $args);
}
/** /**
* Add Language Items - use_db and use_help are assigned where needed (only use them to force inclusion) * Add Language Items - use_db and use_help are assigned where needed (only use them to force inclusion)
* *

View file

@ -394,6 +394,13 @@ $lang = array_merge($lang, array(
'NO_USERS' => 'The requested users do not exist.', 'NO_USERS' => 'The requested users do not exist.',
'NO_USER_SPECIFIED' => 'No username was specified.', 'NO_USER_SPECIFIED' => 'No username was specified.',
// Nullar/Singular/Plural language entry. The key numbers define the number range in which a certain grammatical expression is valid.
'NUM_POSTS_IN_QUEUE' => array(
0 => 'No posts in queue', // 0
1 => '1 post in queue', // 1
2 => '%d posts in queue', // 2+
),
'OCCUPATION' => 'Occupation', 'OCCUPATION' => 'Occupation',
'OFFLINE' => 'Offline', 'OFFLINE' => 'Offline',
'ONLINE' => 'Online', 'ONLINE' => 'Online',