Browse Source

Attempt to fix the singular/plural issue in the po parser

Florian Krämer 8 years ago
parent
commit
cd19ea7f8b

+ 4 - 2
src/I18n/Parser/PoFileParser.php

@@ -24,6 +24,8 @@ namespace Cake\I18n\Parser;
 class PoFileParser
 {
 
+    const PLURAL_PREFIX = 'p:';
+
     /**
      * Parses portable object (PO) format.
      *
@@ -166,9 +168,9 @@ class PoFileParser
             $key = stripcslashes($item['ids']['plural']);
 
             if ($context !== null) {
-                $messages[$key]['_context'][$context] = $plurals;
+                $messages[static::PLURAL_PREFIX . $key]['_context'][$context] = $plurals;
             } else {
-                $messages[$key]['_context'][''] = $plurals;
+                $messages[static::PLURAL_PREFIX . $key]['_context'][''] = $plurals;
             }
         }
     }

+ 14 - 1
src/I18n/Translator.php

@@ -22,6 +22,8 @@ use Aura\Intl\Translator as BaseTranslator;
 class Translator extends BaseTranslator
 {
 
+    const PLURAL_PREFIX = 'p:';
+
     /**
      * Translates the message formatting any placeholders
      *
@@ -33,7 +35,18 @@ class Translator extends BaseTranslator
      */
     public function translate($key, array $tokensValues = [])
     {
-        $message = $this->getMessage($key);
+        if (isset($tokensValues['_count'])) {
+            $message = $this->getMessage(static::PLURAL_PREFIX . $key);
+            if (!$message) {
+                $message = $this->getMessage($key);
+            }
+        } else {
+            $message = $this->getMessage($key);
+            if (!$message) {
+                $message = $this->getMessage(static::PLURAL_PREFIX . $key);
+            }
+        }
+
         if (!$message) {
             // Fallback to the message key
             $message = $key;

+ 16 - 16
tests/TestCase/I18n/Parser/PoFileParserTest.php

@@ -65,8 +65,8 @@ class PoFileParserTest extends TestCase
         $expected = [
             'Plural Rule 1' => [
                 '_context' => [
-                    '' => 'Plural Rule 1 (translated)',
-                ],
+                    '' => 'Plural Rule 1 (translated)'
+                ]
             ],
             '%d = 1' => [
                 '_context' => [
@@ -74,30 +74,30 @@ class PoFileParserTest extends TestCase
                     'Another Context' => '%d = 1 (translated)'
                 ]
             ],
-            '%d = 0 or > 1' => [
+            'p:%d = 0 or > 1' => [
                 '_context' => [
                     'Another Context' => [
-                        0 => '%d = 1 (translated)',
-                        1 => '%d = 0 or > 1 (translated)'
+                        (int)0 => '%d = 1 (translated)',
+                        (int)1 => '%d = 0 or > 1 (translated)'
                     ]
                 ]
             ],
             '%-5d = 1' => [
                 '_context' => [
-                    '' => '%-5d = 1 (translated)',
-                ],
+                    '' => '%-5d = 1 (translated)'
+                ]
             ],
-            '%-5d = 0 or > 1' => [
+            'p:%-5d = 0 or > 1' => [
                 '_context' => [
                     '' => [
-                        0 => '%-5d = 1 (translated)',
-                        1 => '',
-                        2 => '',
-                        3 => '',
-                        4 => '%-5d = 0 or > 1 (translated)'
-                    ],
-                ],
-            ],
+                        (int)0 => '%-5d = 1 (translated)',
+                        (int)1 => '',
+                        (int)2 => '',
+                        (int)3 => '',
+                        (int)4 => '%-5d = 0 or > 1 (translated)'
+                    ]
+                ]
+            ]
         ];
         $this->assertEquals($expected, $messages);
     }