Просмотр исходного кода

Fixes #66 - Gettext adapter cannot translate plurals when the target language has only one plural form

Frank Brückner 12 лет назад
Родитель
Сommit
4a7f3c8909

+ 1 - 1
library/Zend/Translate/Adapter/Gettext.php

@@ -122,7 +122,7 @@ class Zend_Translate_Adapter_Gettext extends Zend_Translate_Adapter {
                 fseek($this->_file, $transtemp[$count * 2 + 2]);
                 $translate = fread($this->_file, $transtemp[$count * 2 + 1]);
                 $translate = explode("\0", $translate);
-                if ((count($original) > 1) && (count($translate) > 1)) {
+                if ((count($original) > 1)) {
                     $this->_data[$locale][$original[0]] = $translate;
                     array_shift($original);
                     foreach ($original as $orig) {

+ 46 - 0
tests/Zend/Translate/Adapter/GettextTest.php

@@ -255,6 +255,52 @@ class Zend_Translate_Adapter_GettextTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group GH-66
+     */
+    public function testPluralToPlural()
+    {
+        $adapter = new Zend_Translate_Adapter_Gettext(
+            dirname(__FILE__) . '/_files/translation_plural_fr.mo', 'fr'
+        );
+
+        $this->assertEquals(
+            'Il ya %d message',
+            $adapter->plural('There is %d message', 'There are %d messages', 0)
+        );
+        $this->assertEquals(
+            'Il ya %d message',
+            $adapter->plural('There is %d message', 'There are %d messages', 1)
+        );
+        $this->assertEquals(
+            'Il ya %d messages',
+            $adapter->plural('There is %d message', 'There are %d messages', 2)
+        );
+    }
+
+    /**
+     * @group GH-66
+     */
+    public function testPluralToSingular()
+    {
+        $adapter = new Zend_Translate_Adapter_Gettext(
+            dirname(__FILE__) . '/_files/translation_plural_tr.mo', 'tr'
+        );
+
+        $this->assertEquals(
+            '%d mesaj var',
+            $adapter->plural('There is %d message', 'There are %d messages', 0)
+        );
+        $this->assertEquals(
+            '%d mesaj var',
+            $adapter->plural('There is %d message', 'There are %d messages', 1)
+        );
+        $this->assertEquals(
+            '%d mesaj var',
+            $adapter->plural('There is %d message', 'There are %d messages', 2)
+        );
+    }
+
+    /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *
      * @param  integer $errno

BIN
tests/Zend/Translate/Adapter/_files/translation_plural_fr.mo


+ 18 - 0
tests/Zend/Translate/Adapter/_files/translation_plural_fr.po

@@ -0,0 +1,18 @@
+msgid ""
+msgstr ""
+"Content-Type: text/plain; charset=UTF-8\n"
+"Plural-Forms: nplurals=2; plural=n > 1;\n"
+"Project-Id-Version: \n"
+"POT-Creation-Date: \n"
+"PO-Revision-Date: \n"
+"Last-Translator: \n"
+"Language-Team: \n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Language: fr\n"
+"X-Generator: Poedit 1.6.3\n"
+
+msgid "There is %d message"
+msgid_plural "There are %d messages"
+msgstr[0] "Il ya %d message"
+msgstr[1] "Il ya %d messages"

BIN
tests/Zend/Translate/Adapter/_files/translation_plural_tr.mo


+ 17 - 0
tests/Zend/Translate/Adapter/_files/translation_plural_tr.po

@@ -0,0 +1,17 @@
+msgid ""
+msgstr ""
+"Content-Type: text/plain; charset=UTF-8\n"
+"Plural-Forms: nplurals=1; plural=0;\n"
+"Project-Id-Version: \n"
+"POT-Creation-Date: \n"
+"PO-Revision-Date: \n"
+"Last-Translator: \n"
+"Language-Team: \n"
+"MIME-Version: 1.0\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Language: tr\n"
+"X-Generator: Poedit 1.6.3\n"
+
+msgid "There is %d message"
+msgid_plural "There are %d message"
+msgstr[0] "%d mesaj var"