From 25c60c313a530cf990868294ae5f7202bb2a2aed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Sun, 16 Sep 2012 19:13:03 +0000 Subject: [PATCH] Support explicit 0 and 1 forms for plural in PHP rails-i18n has the same, lets see if this is flexible enough or whether we need to allow more complex expressions. Change-Id: I50eb0c6d1c02ca936848d310de625ed1fe43d91a --- languages/Language.php | 12 ++++++++++++ tests/phpunit/languages/LanguageTest.php | 25 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/languages/Language.php b/languages/Language.php index 40d1f36cd9..c315763c7f 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3409,6 +3409,18 @@ class Language { if ( !count( $forms ) ) { return ''; } + + // Handle explicit 0= and 1= forms + foreach ( $forms as $index => $form ) { + if ( isset( $form[1] ) && $form[1] === '=' ) { + if ( $form[0] === (string) $count ) { + return substr( $form, 2 ); + } + unset( $forms[$index] ); + } + } + $forms = array_values( $forms ); + $pluralForm = $this->getPluralForm( $count ); $pluralForm = min( $pluralForm, count( $forms ) - 1 ); return $forms[$pluralForm]; diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 70254490d6..bfb45c75c6 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -1065,5 +1065,30 @@ class LanguageTest extends MediaWikiTestCase { array( 10000, 'MMMMMMMMMM' ), ); } + + /** + * @dataProvider providePluralData + */ + function testConvertPlural( $expected, $number, $forms ) { + $chosen = $this->lang->convertPlural( $number, $forms ); + $this->assertEquals( $expected, $chosen ); + } + + function providePluralData() { + return array( + array( 'explicit zero', 0, array( + '0=explicit zero', 'singular', 'plural' + ) ), + array( 'explicit one', 1, array( + 'singular', 'plural', '1=explicit one', + ) ), + array( 'singular', 1, array( + 'singular', 'plural', '0=explicit zero', + ) ), + array( 'plural', 3, array( + '0=explicit zero', '1=explicit one', 'singular', 'plural' + ) ), + ); + } } -- 2.20.1