Browse Source

Backport sprintf formatter fix (Issue #15288).

See also https://github.com/cakephp/cakephp/pull/15289
Waldemar Bartikowski 5 years ago
parent
commit
1bab22cc29

+ 0 - 19
src/I18n/Formatter/IcuFormatter.php

@@ -37,25 +37,6 @@ class IcuFormatter implements FormatterInterface
      */
     public function format($locale, $message, array $vars)
     {
-        unset($vars['_singular'], $vars['_count']);
-
-        return $this->_formatMessage($locale, $message, $vars);
-    }
-
-    /**
-     * Does the actual formatting using the MessageFormatter class
-     *
-     * @param string $locale The locale in which the message is presented.
-     * @param string|array $message The message to be translated
-     * @param array $vars The list of values to interpolate in the message
-     * @return string The formatted message
-     * @throws \Aura\Intl\Exception\CannotInstantiateFormatter if any error occurred
-     * while parsing the message
-     * @throws \Aura\Intl\Exception\CannotFormat If any error related to the passed
-     * variables is found
-     */
-    protected function _formatMessage($locale, $message, $vars)
-    {
         if ($message === '') {
             return $message;
         }

+ 0 - 2
src/I18n/Formatter/SprintfFormatter.php

@@ -33,8 +33,6 @@ class SprintfFormatter implements FormatterInterface
      */
     public function format($locale, $message, array $vars)
     {
-        unset($vars['_singular']);
-
         return vsprintf($message, $vars);
     }
 }

+ 2 - 0
src/I18n/Translator.php

@@ -84,6 +84,8 @@ class Translator extends BaseTranslator
             $message = $key;
         }
 
+        unset($tokensValues['_count'], $tokensValues['_singular']);
+
         return $this->formatter->format($this->locale, $message, $tokensValues);
     }
 

+ 9 - 9
tests/TestCase/I18n/I18nTest.php

@@ -106,14 +106,14 @@ class I18nTest extends TestCase
      *
      * @return void
      */
-    public function testPluralSelection()
+    public function testPluralSelectionSprintfFormatter()
     {
         I18n::setDefaultFormatter('sprintf');
         $translator = I18n::getTranslator(); // en_US
-        $result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
+        $result = $translator->translate('%d = 0 or > 1', ['_count' => 1, 1]);
         $this->assertEquals('1 is 1 (po translated)', $result);
 
-        $result = $translator->translate('%d = 0 or > 1', ['_count' => 2]);
+        $result = $translator->translate('%d = 0 or > 1', ['_count' => 2, 2]);
         $this->assertEquals('2 is 2-4 (po translated)', $result);
     }
 
@@ -315,17 +315,17 @@ class I18nTest extends TestCase
     public function testBasicTranslatePluralFunction()
     {
         I18n::setDefaultFormatter('sprintf');
-        $result = __n('singular msg', '%d = 0 or > 1', 1);
+        $result = __n('singular msg', '%d = 0 or > 1', 1, 1);
         $this->assertEquals('1 is 1 (po translated)', $result);
 
-        $result = __n('singular msg', '%d = 0 or > 1', 2);
+        $result = __n('singular msg', '%d = 0 or > 1', 2, 2);
         $this->assertEquals('2 is 2-4 (po translated)', $result);
 
-        $result = __n('%s %s and %s are good', '%s and %s are best', 1, ['red', 'blue']);
-        $this->assertEquals('1 red and blue are good', $result);
+        $result = __n('%s, %s, and %s are good', '%s, %s, and %s are best', 1, ['red', 'blue', 'green']);
+        $this->assertSame('red, blue, and green are good', $result);
 
-        $result = __n('%s %s and %s are good', '%s and %s are best', 1, 'red', 'blue');
-        $this->assertEquals('1 red and blue are good', $result);
+        $result = __n('%s, %s, and %s are good', '%s, %s, and %s are best', 1, 'red', 'blue', 'green');
+        $this->assertSame('red, blue, and green are good', $result);
     }
 
     /**