Skip to content

Commit

Permalink
Fix plural calculation for Arabic.
Browse files Browse the repository at this point in the history
Refs #5348
  • Loading branch information
ADmad committed Dec 8, 2014
1 parent 11981f8 commit 850edf4
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/Cake/I18n/I18n.php
Expand Up @@ -311,6 +311,7 @@ public static function domains() {
* @param string $header Type
* @param int $n Number
* @return int plural match
* @see
*/
protected function _pluralGuess($header, $n) {
if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
Expand Down Expand Up @@ -351,7 +352,15 @@ protected function _pluralGuess($header, $n) {
}
} elseif (strpos($header, "plurals=5")) {
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
} elseif (strpos($header, "plurals=6")) {
return $n == 0 ? 0 :
($n == 1 ? 1 :
($n == 2 ? 2 :
($n % 100 >= 3 && $n % 100 <= 10 ? 3 :
($n % 100 >= 11 ? 4 : 5))));
}

return 0;
}

/**
Expand Down
131 changes: 127 additions & 4 deletions lib/Cake/Test/Case/I18n/I18nTest.php
Expand Up @@ -1512,6 +1512,127 @@ public function assertRulesFourteen() {
$this->assertTrue(in_array('25 everything else (from core translated)', $corePlurals));
}

/**
* testMoRulesFifteen method
*
* @return void
*/
public function testMoRulesFifteen() {
Configure::write('Config.language', 'rule_15_mo');
$this->assertRulesFifteen();
}

/**
* testPoRulesFifteen method
*
* @return void
*/
public function testPoRulesFifteen() {
Configure::write('Config.language', 'rule_15_po');
$this->assertRulesFifteen();
}

/**
* Assertions for plural rules fifteen
*
* @return void
*/
public function assertRulesFifteen() {
$singular = $this->_singular();
$this->assertEquals('Plural Rule 15 (translated)', $singular);

$plurals = $this->_plural(111);
$this->assertTrue(in_array('0 is 0 (translated)', $plurals));
$this->assertTrue(in_array('1 is 1 (translated)', $plurals));
$this->assertTrue(in_array('2 is 2 (translated)', $plurals));
$this->assertTrue(in_array('3 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('4 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('5 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('6 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('7 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('8 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('9 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('10 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('11 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('12 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('13 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('14 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('15 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('16 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('17 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('18 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('19 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('20 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('31 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('42 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('53 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('64 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('75 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('86 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('97 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('98 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('99 ends with 11-99 (translated)', $plurals));
$this->assertTrue(in_array('100 everything else (translated)', $plurals));
$this->assertTrue(in_array('101 everything else (translated)', $plurals));
$this->assertTrue(in_array('102 everything else (translated)', $plurals));
$this->assertTrue(in_array('103 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('104 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('105 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('106 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('107 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('108 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('109 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('110 ends with 03-10 (translated)', $plurals));
$this->assertTrue(in_array('111 ends with 11-99 (translated)', $plurals));

$coreSingular = $this->_singularFromCore();
$this->assertEquals('Plural Rule 15 (from core translated)', $coreSingular);

$corePlurals = $this->_pluralFromCore(111);
$this->assertTrue(in_array('0 is 0 (from core translated)', $corePlurals));
$this->assertTrue(in_array('1 is 1 (from core translated)', $corePlurals));
$this->assertTrue(in_array('2 is 2 (from core translated)', $corePlurals));
$this->assertTrue(in_array('3 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('4 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('5 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('6 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('7 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('8 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('9 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('10 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('11 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('12 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('13 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('14 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('15 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('16 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('17 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('18 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('19 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('20 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('31 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('42 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('53 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('64 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('75 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('86 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('97 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('98 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('99 ends with 11-99 (from core translated)', $corePlurals));
$this->assertTrue(in_array('100 everything else (from core translated)', $corePlurals));
$this->assertTrue(in_array('101 everything else (from core translated)', $corePlurals));
$this->assertTrue(in_array('102 everything else (from core translated)', $corePlurals));
$this->assertTrue(in_array('103 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('104 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('105 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('106 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('107 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('108 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('109 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('110 ends with 03-10 (from core translated)', $corePlurals));
$this->assertTrue(in_array('111 ends with 11-99 (from core translated)', $corePlurals));
}

/**
* testSetLanguageWithSession method
*
Expand Down Expand Up @@ -1948,11 +2069,12 @@ protected function _singular() {
/**
* Plural method
*
* @param int $upTo For numbers upto (default to 25)
* @return void
*/
protected function _plural() {
protected function _plural($upTo = 25) {
$plurals = array();
for ($number = 0; $number <= 25; $number++) {
for ($number = 0; $number <= $upTo; $number++) {
$plurals[] = sprintf(__n('%d = 1', '%d = 0 or > 1', (float)$number), (float)$number);
}
return $plurals;
Expand All @@ -1971,11 +2093,12 @@ protected function _singularFromCore() {
/**
* pluralFromCore method
*
* @param int $upTo For numbers upto (default to 25)
* @return void
*/
protected function _pluralFromCore() {
protected function _pluralFromCore($upTo = 25) {
$plurals = array();
for ($number = 0; $number <= 25; $number++) {
for ($number = 0; $number <= $upTo; $number++) {
$plurals[] = sprintf(__n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', (float)$number), (float)$number);
}
return $plurals;
Expand Down
Binary file not shown.
Binary file not shown.
25 changes: 25 additions & 0 deletions lib/Cake/Test/test_app/Locale/rule_15_po/LC_MESSAGES/core.po
@@ -0,0 +1,25 @@
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Testsuite\n"
"POT-Creation-Date: 2014-12-06 19:20-0300\n"
"PO-Revision-Date: \n"
"Language-Team: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n"
"X-Poedit-Language: Six Forms of Plurals\n"
"X-Poedit-SourceCharset: utf-8\n"

msgid "Plural Rule 1 (from core)"
msgstr "Plural Rule 15 (from core translated)"

msgid "%d = 1 (from core)"
msgid_plural "%d = 0 or > 1 (from core)"
msgstr[0] "%d is 0 (from core translated)"
msgstr[1] "%d is 1 (from core translated)"
msgstr[2] "%d is 2 (from core translated)"
msgstr[3] "%d ends with 03-10 (from core translated)"
msgstr[4] "%d ends with 11-99 (from core translated)"
msgstr[5] "%d everything else (from core translated)"

25 changes: 25 additions & 0 deletions lib/Cake/Test/test_app/Locale/rule_15_po/LC_MESSAGES/default.po
@@ -0,0 +1,25 @@
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Testsuite\n"
"POT-Creation-Date: 2014-12-06 19:20-0300\n"
"PO-Revision-Date: \n"
"Language-Team: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n"
"X-Poedit-Language: Six Forms of Plurals\n"
"X-Poedit-SourceCharset: utf-8\n"

msgid "Plural Rule 1"
msgstr "Plural Rule 15 (translated)"

msgid "%d = 1"
msgid_plural "%d = 0 or > 1"
msgstr[0] "%d is 0 (translated)"
msgstr[1] "%d is 1 (translated)"
msgstr[2] "%d is 2 (translated)"
msgstr[3] "%d ends with 03-10 (translated)"
msgstr[4] "%d ends with 11-99 (translated)"
msgstr[5] "%d everything else (translated)"

0 comments on commit 850edf4

Please sign in to comment.