diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2011-09-21 00:36:18 +0200 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2011-11-25 15:10:51 -0500 |
commit | 33e3bf4f64c3c2734bf97bcb204255cfdab37773 (patch) | |
tree | 93e84936d6361c7b44efbca949e7ecdfeafdb1f6 | |
parent | 7da6826a671cfce61f7d92c2f5b1deafa4865872 (diff) | |
download | forums-33e3bf4f64c3c2734bf97bcb204255cfdab37773.tar forums-33e3bf4f64c3c2734bf97bcb204255cfdab37773.tar.gz forums-33e3bf4f64c3c2734bf97bcb204255cfdab37773.tar.bz2 forums-33e3bf4f64c3c2734bf97bcb204255cfdab37773.tar.xz forums-33e3bf4f64c3c2734bf97bcb204255cfdab37773.zip |
[ticket/10345] Make the use of the 0-case optional
And correctly determinate the rule otherwise
PHPBB3-10345
-rw-r--r-- | phpBB/includes/session.php | 74 | ||||
-rw-r--r-- | tests/user/lang_test.php | 9 |
2 files changed, 48 insertions, 35 deletions
diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index efb2059922..c78d0f819a 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -1871,20 +1871,30 @@ class user extends session { if (is_int($args[$i]) || is_float($args[$i])) { - $use_plural_form = $this->get_plural_form($args[$i]); - $numbers = array_keys($lang); - - foreach ($numbers as $num) + if ($args[$i] == 0 && isset($lang[0])) + { + // We allow each translation using plural forms to specify a version for the case of 0 things, + // so that "0 users" may be displayed as "No users". + $key_found = 0; + break; + } + else { - if ($num > $use_plural_form) + $use_plural_form = $this->get_plural_form($args[$i]); + $numbers = array_keys($lang); + + foreach ($numbers as $num) { - // If the key we need to use does not exist, we fall back to the previous one. - break; - } + if ($num > $use_plural_form) + { + // If the key we need to use does not exist, we fall back to the previous one. + break; + } - $key_found = $num; + $key_found = $num; + } + break; } - break; } } @@ -1913,12 +1923,6 @@ class user extends session { $number = (int) $number; - if ($number == 0) - { - // We allow each translation using plural forms to specify a version for the case of 0 things, so that "0 users" may be displayed as "No users". - return 0; - } - // Default to English system $plural_rule = ($force_rule !== false) ? $force_rule : ((isset($this->lang['PLURAL_RULE'])) ? $this->lang['PLURAL_RULE'] : 1); if ($plural_rule > 15 || $plural_rule < 0) @@ -1935,7 +1939,7 @@ class user extends session case 0: /** * Families: Asian (Chinese, Japanese, Korean, Vietnamese), Persian, Turkic/Altaic (Turkish), Thai, Lao - * 1 - everything: 1, 2, ... + * 1 - everything: 0, 1, 2, ... */ return 1; @@ -1943,25 +1947,26 @@ class user extends session /** * Families: Germanic (Danish, Dutch, English, Faroese, Frisian, German, Norwegian, Swedish), Finno-Ugric (Estonian, Finnish, Hungarian), Language isolate (Basque), Latin/Greek (Greek), Semitic (Hebrew), Romanic (Italian, Portuguese, Spanish, Catalan) * 1 - 1 - * 2 - everything else: 2, 3, ... + * 2 - everything else: 0, 2, 3, ... */ return ($number == 1) ? 1 : 2; case 2: /** * Families: Romanic (French, Brazilian Portuguese) - * 1 - 1 normaly this would also apply to 0 + * 1 - 0, 1 * 2 - everything else: 2, 3, ... */ - return ($number == 1) ? 1 : 2; + return (($number == 0) || ($number == 1)) ? 1 : 2; case 3: /** * Families: Baltic (Latvian) - * 1 - ends in 1, not 11: 1, 21, ... 101, 121, ... - * 2 - everything else: 2, 3, ... 10, 11, 12, ... 20, 22, ... + * 1 - 0 + * 2 - ends in 1, not 11: 1, 21, ... 101, 121, ... + * 3 - everything else: 2, 3, ... 10, 11, 12, ... 20, 22, ... */ - return (($number % 10 == 1) && ($number % 100 != 11)) ? 1 : 2; + return ($number == 0) ? 1 : ((($number % 10 == 1) && ($number % 100 != 11)) ? 2 : 3); case 4: /** @@ -1969,7 +1974,7 @@ class user extends session * 1 - is 1 or 11: 1, 11 * 2 - is 2 or 12: 2, 12 * 3 - others between 3 and 19: 3, 4, ... 10, 13, ... 18, 19 - * 4 - everything else: 20, 21, ... + * 4 - everything else: 0, 20, 21, ... */ return ($number == 1 || $number == 11) ? 1 : (($number == 2 || $number == 12) ? 2 : (($number >= 3 && $number <= 19) ? 3 : 4)); @@ -1977,7 +1982,7 @@ class user extends session /** * Families: Romanic (Romanian) * 1 - 1 - * 2 - ends in 01-19: 2, 3, ... 19, 101, 102, ... 119, 201, ... + * 2 - is 0 or ends in 01-19: 0, 2, 3, ... 19, 101, 102, ... 119, 201, ... * 3 - everything else: 20, 21, ... */ return ($number == 1) ? 1 : ((($number == 0) || (($number % 100 > 0) && ($number % 100 < 20))) ? 2 : 3); @@ -1986,7 +1991,7 @@ class user extends session /** * Families: Baltic (Lithuanian) * 1 - ends in 1, not 11: 1, 21, 31, ... 101, 121, ... - * 2 - ends in 0 or ends in 10-20: 10, 11, 12, ... 19, 20, 30, 40, ... + * 2 - ends in 0 or ends in 10-20: 0, 10, 11, 12, ... 19, 20, 30, 40, ... * 3 - everything else: 2, 3, ... 8, 9, 22, 23, ... 29, 32, 33, ... */ return (($number % 10 == 1) && ($number % 100 != 11)) ? 1 : ((($number % 10 < 2) || (($number % 100 >= 10) && ($number % 100 < 20))) ? 2 : 3); @@ -1996,7 +2001,7 @@ class user extends session * Families: Slavic (Croatian, Serbian, Russian, Ukrainian) * 1 - ends in 1, not 11: 1, 21, 31, ... 101, 121, ... * 2 - ends in 2-4, not 12-14: 2, 3, 4, 22, 23, 24, 32, ... - * 3 - everything else: 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26, ... + * 3 - everything else: 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26, ... */ return (($number % 10 == 1) && ($number % 100 != 11)) ? 1 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 2 : 3); @@ -2005,7 +2010,7 @@ class user extends session * Families: Slavic (Slovak, Czech) * 1 - 1 * 2 - 2, 3, 4 - * 3 - everything else: 5, 6, 7, ... + * 3 - everything else: 0, 5, 6, 7, ... */ return ($number == 1) ? 1 : ((($number >= 2) && ($number <= 4)) ? 2 : 3); @@ -2014,7 +2019,7 @@ class user extends session * Families: Slavic (Polish) * 1 - 1 * 2 - ends in 2-4, not 12-14: 2, 3, 4, 22, 23, 24, 32, ... 104, 122, ... - * 3 - everything else: 5, 6, ... 11, 12, 13, 14, 15, ... 20, 21, 25, ... + * 3 - everything else: 0, 5, 6, ... 11, 12, 13, 14, 15, ... 20, 21, 25, ... */ return ($number == 1) ? 1 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 2 : 3); @@ -2024,7 +2029,7 @@ class user extends session * 1 - ends in 01: 1, 101, 201, ... * 2 - ends in 02: 2, 102, 202, ... * 3 - ends in 03-04: 3, 4, 103, 104, 203, 204, ... - * 4 - everything else: 5, 6, 7, 8, 9, 10, 11, ... + * 4 - everything else: 0, 5, 6, 7, 8, 9, 10, 11, ... */ return ($number % 100 == 1) ? 1 : (($number % 100 == 2) ? 2 : ((($number % 100 == 3) || ($number % 100 == 4)) ? 3 : 4)); @@ -2047,14 +2052,15 @@ class user extends session * 3 - ends in 03-10: 3, 4, ... 10, 103, 104, ... 110, 203, 204, ... * 4 - ends in 11-99: 11, ... 99, 111, 112, ... * 5 - everything else: 100, 101, 102, 200, 201, 202, ... + * 6 - 0 */ - return ($number == 1) ? 1 : (($number == 2) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5))); + return ($number == 1) ? 1 : (($number == 2) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : (($number != 0) ? 5 : 6)))); case 13: /** * Families: Semitic (Maltese) * 1 - 1 - * 2 - ends in 01-10: 2, 3, ... 9, 10, 101, 102, ... + * 2 - is 0 or ends in 01-10: 0, 2, 3, ... 9, 10, 101, 102, ... * 3 - ends in 11-19: 11, 12, ... 18, 19, 111, 112, ... * 4 - everything else: 20, 21, ... */ @@ -2065,7 +2071,7 @@ class user extends session * Families: Slavic (Macedonian) * 1 - ends in 1: 1, 11, 21, ... * 2 - ends in 2: 2, 12, 22, ... - * 3 - everything else: 3, 4, ... 10, 13, 14, ... 20, 23, ... + * 3 - everything else: 0, 3, 4, ... 10, 13, 14, ... 20, 23, ... */ return ($number % 10 == 1) ? 1 : (($number % 10 == 2) ? 2 : 3); @@ -2073,7 +2079,7 @@ class user extends session /** * Families: Icelandic * 1 - ends in 1, not 11: 1, 21, 31, ... 101, 121, 131, ... - * 2 - everything else: 2, 3, ... 10, 11, 12, ... 20, 22, ... + * 2 - everything else: 0, 2, 3, ... 10, 11, 12, ... 20, 22, ... */ return (($number % 10 == 1) && ($number % 100 != 11)) ? 1 : 2; } diff --git a/tests/user/lang_test.php b/tests/user/lang_test.php index ffc3ea1302..bf36811f89 100644 --- a/tests/user/lang_test.php +++ b/tests/user/lang_test.php @@ -30,6 +30,10 @@ class phpbb_user_lang_test extends phpbb_test_case 1 => '1 post', // 1 2 => '%d posts', // 0, 2+ ), + 'ARRY_MISSING' => array( + 1 => '%d post', // 1 + //Missing second plural + ), 'ARRY_FLOAT' => array( 1 => '1 post', // 1.x 2 => '%1$.1f posts', // 0.x, 2+.x @@ -65,11 +69,14 @@ class phpbb_user_lang_test extends phpbb_test_case $this->assertEquals($user->lang('ARRY', 2), '2 posts'); $this->assertEquals($user->lang('ARRY', 123), '123 posts'); - // Array with missing keys + // No 0 key defined $this->assertEquals($user->lang('ARRY_NO_ZERO', 0), '0 posts'); $this->assertEquals($user->lang('ARRY_NO_ZERO', 1), '1 post'); $this->assertEquals($user->lang('ARRY_NO_ZERO', 2), '2 posts'); + // Array with missing keys + $this->assertEquals($user->lang('ARRY_MISSING', 2), '2 post'); + // Floats as array key $this->assertEquals($user->lang('ARRY_FLOAT', 1.3), '1 post'); $this->assertEquals($user->lang('ARRY_FLOAT', 2.0), '2.0 posts'); |