From: Pavel Selitskas
Date: Tue, 3 Dec 2013 08:37:14 +0000 (+0530)
Subject: Handle explicit plural forms in custom convertPlural in language classes
X-Git-Tag: 1.31.0-rc.0~17822^2
X-Git-Url: http://git.cyclocoop.org/%22.htmlspecialchars%28%24url_syndic%29.%22?a=commitdiff_plain;h=81fc875c0b203ece493cbc0fdddbb9b9c68f5dd0;p=lhc%2Fweb%2Fwiklou.git
Handle explicit plural forms in custom convertPlural in language classes
A new protected method looks for explicitly defined forms.
Every overriden language class is required to use this method.
Includes tests.
Redoing old patch I6dc759e3dfb05d6673209ba00da6592a384d5300
Bug: 46422
Change-Id: I2a9f93567087babb896999f1214d3c56afc67c96
---
diff --git a/languages/Language.php b/languages/Language.php
index dc1fc7de9d..0026fdf4bf 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -3632,17 +3632,10 @@ class Language {
*/
function convertPlural( $count, $forms ) {
// Handle explicit n=pluralform cases
- foreach ( $forms as $index => $form ) {
- if ( preg_match( '/^\d+=/i', $form ) ) {
- $pos = strpos( $form, '=' );
- if ( substr( $form, 0, $pos ) === (string)$count ) {
- return substr( $form, $pos + 1 );
- }
- unset( $forms[$index] );
- }
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
}
-
- $forms = array_values( $forms );
if ( !count( $forms ) ) {
return '';
}
@@ -3652,6 +3645,34 @@ class Language {
return $forms[$pluralForm];
}
+ /**
+ * Handles explicit plural forms for Language::convertPlural()
+ *
+ * In {{PLURAL:$1|0=nothing|one|many}}, 0=nothing will be returned if $1 equals zero.
+ * If an explicitly defined plural form matches the $count, then
+ * string value returned, otherwise array returned for further consideration
+ * by CLDR rules or overridden convertPlural().
+ *
+ * @since 1.23
+ *
+ * @param int $count non-localized number
+ * @param array $forms different plural forms
+ *
+ * @return array|string
+ */
+ protected function handleExplicitPluralForms( $count, array $forms ) {
+ foreach ( $forms as $index => $form ) {
+ if ( preg_match( '/\d+=/i', $form ) ) {
+ $pos = strpos( $form, '=' );
+ if ( substr( $form, 0, $pos ) === (string) $count ) {
+ return substr( $form, $pos + 1 );
+ }
+ unset( $forms[$index] );
+ }
+ }
+ return array_values( $forms );
+ }
+
/**
* Checks that convertPlural was given an array and pads it to requested
* amount of forms by copying the last one.
diff --git a/languages/classes/LanguageBe_tarask.php b/languages/classes/LanguageBe_tarask.php
index 6497b50c11..d3e78fe68e 100644
--- a/languages/classes/LanguageBe_tarask.php
+++ b/languages/classes/LanguageBe_tarask.php
@@ -46,6 +46,10 @@ class LanguageBe_tarask extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageGv.php b/languages/classes/LanguageGv.php
index ba8a53cef1..23a29162d2 100644
--- a/languages/classes/LanguageGv.php
+++ b/languages/classes/LanguageGv.php
@@ -35,6 +35,10 @@ class LanguageGv extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageHr.php b/languages/classes/LanguageHr.php
index 910bc100df..0c65ec79f8 100644
--- a/languages/classes/LanguageHr.php
+++ b/languages/classes/LanguageHr.php
@@ -34,6 +34,10 @@ class LanguageHr extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageKsh.php b/languages/classes/LanguageKsh.php
index 09c5727909..d25ce7147b 100644
--- a/languages/classes/LanguageKsh.php
+++ b/languages/classes/LanguageKsh.php
@@ -189,6 +189,10 @@ class LanguageKsh extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageSr.php b/languages/classes/LanguageSr.php
index 6f0ff4b533..7310e8e02f 100644
--- a/languages/classes/LanguageSr.php
+++ b/languages/classes/LanguageSr.php
@@ -246,6 +246,10 @@ class LanguageSr extends LanguageSr_ec {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageSr_ec.php b/languages/classes/LanguageSr_ec.php
index cb9e81917d..4787856217 100644
--- a/languages/classes/LanguageSr_ec.php
+++ b/languages/classes/LanguageSr_ec.php
@@ -34,6 +34,10 @@ class LanguageSr_ec extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageSr_el.php b/languages/classes/LanguageSr_el.php
index 99d12935c8..3f086df436 100644
--- a/languages/classes/LanguageSr_el.php
+++ b/languages/classes/LanguageSr_el.php
@@ -34,6 +34,10 @@ class LanguageSr_el extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/languages/classes/LanguageUk.php b/languages/classes/LanguageUk.php
index 4ad272ba90..aabe3902b8 100644
--- a/languages/classes/LanguageUk.php
+++ b/languages/classes/LanguageUk.php
@@ -92,6 +92,10 @@ class LanguageUk extends Language {
* @return string
*/
function convertPlural( $count, $forms ) {
+ $forms = $this->handleExplicitPluralForms( $count, $forms );
+ if ( is_string( $forms ) ) {
+ return $forms;
+ }
if ( !count( $forms ) ) {
return '';
}
diff --git a/tests/phpunit/languages/LanguageBe_taraskTest.php b/tests/phpunit/languages/LanguageBe_taraskTest.php
index d5822f4a11..97a17ec030 100644
--- a/tests/phpunit/languages/LanguageBe_taraskTest.php
+++ b/tests/phpunit/languages/LanguageBe_taraskTest.php
@@ -79,12 +79,13 @@ class LanguageBe_taraskTest extends LanguageClassesTestCase {
* @covers Language::convertPlural
*/
public function testPluralTwoForms( $result, $value ) {
- $forms = array( 'one', 'other' );
+ $forms = array( 'one', 'other', '0=one' );
$this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
}
public static function providePluralTwoForms() {
return array(
+ array( 'one', 0 ),
array( 'one', 1 ),
array( 'other', 11 ),
array( 'other', 91 ),
diff --git a/tests/phpunit/languages/LanguageUkTest.php b/tests/phpunit/languages/LanguageUkTest.php
index 0783fcf6af..1d81bc5fec 100644
--- a/tests/phpunit/languages/LanguageUkTest.php
+++ b/tests/phpunit/languages/LanguageUkTest.php
@@ -17,6 +17,17 @@ class LanguageUkTest extends LanguageClassesTestCase {
$this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
}
+ /**
+ * Test explicit plural forms - n=FormN forms
+ * @covers Language::convertPlural
+ */
+ public function testExplicitPlural() {
+ $forms = array( 'one', 'few', 'many', 'other', '12=dozen' );
+ $this->assertEquals( 'dozen', $this->getLang()->convertPlural( 12, $forms ) );
+ $forms = array( 'one', 'few', 'many', '100=hundred', 'other', '12=dozen' );
+ $this->assertEquals( 'hundred', $this->getLang()->convertPlural( 100, $forms ) );
+ }
+
/**
* @dataProvider providePlural
* @covers Language::getPluralRuleType