ソースを参照

Added test for SprintfFormatter

Jose Lorenzo Rodriguez 11 年 前
コミット
094790e47f

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

@@ -18,12 +18,26 @@ use Aura\Intl\FormatterInterface;
 use Cake\I18n\PluralRules;
 
 /**
- *
+ * A formatter that will interpolate variables using sprintf and
+ * select the correct plural form when required
  */
 class SprintfFormatter implements FormatterInterface {
 
+/**
+ * Returns a string with all passed variables interpolated into the original
+ * message. Variables are interpolated using the sprintf format.
+ *
+ * If an array is passed in `$message`, it will trigger the plural selection
+ * routine. Plural forms are selected depending on the locale and the `_count`
+ * key passed in `$vars`.
+ *
+ * @param string $locale The locale in which the message is presented.
+ * @param string|array $message The message to be translated
+ * @return string The formatted message
+ */
 	public function format($locale, $message, array $vars) {
-		if (isset($vars['_count']) && !is_string($message)) {
+		if (!is_string($message)) {
+			$count = isset($vars['_count']) ? $vars['_count'] : 0;
 			$form = PluralRules::calculate($locale, $vars['_count']);
 			$message = $message[$form];
 		}

+ 50 - 0
tests/TestCase/I18n/Formatter/SprintfFormatterTest.php

@@ -0,0 +1,50 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\I18n;
+
+use Cake\I18n\Formatter\SprintfFormatter;
+use Cake\TestSuite\TestCase;
+
+/**
+ * SprintfFormatter tests
+ *
+ */
+class SprintfFormatterTest extends TestCase {
+
+/**
+ * Tests that variables are interpolated correctly
+ *
+ * @return void
+ */
+	public function testFormatSimple() {
+		$formatter = new SprintfFormatter();
+		$this->assertEquals('Hello José', $formatter->format('en_US', 'Hello %s', ['José']));
+		$this->assertEquals('1 Orange', $formatter->format('en_US', '%d %s', [1, 'Orange']));
+	}
+
+/**
+ * Tests that plural forms are selected for the passed locale
+ *
+ * @return void
+ */
+	public function testFormatPlural() {
+		$formatter = new SprintfFormatter();
+		$messages = ['%d is 0', '%d is 1', '%d is 2', '%d is 3', '%d > 11'];
+		$this->assertEquals('1 is 1', $formatter->format('ar', $messages, ['_count' => 1]));
+		$this->assertEquals('2 is 2', $formatter->format('ar', $messages, ['_count' => 2]));
+		$this->assertEquals('20 > 11', $formatter->format('ar', $messages, ['_count' => 20]));
+	}
+
+}

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

@@ -48,7 +48,7 @@ class I18nTest extends TestCase {
 	}
 
 /**
- * Tests that plural rules are correctly used for the english language
+ * Tests that plural rules are correctly used for the English language
  *
  * @return void
  */

+ 6 - 0
tests/TestCase/I18n/PluralRulesTest.php

@@ -23,6 +23,12 @@ use Cake\TestSuite\TestCase;
  */
 class PluralRulesTest extends TestCase {
 
+/**
+ * Returns the notable combinations for locales and numbers
+ * with the respective plural form that should be selected
+ *
+ * @return array
+ */
 	public function localesProvider() {
 		return [
 			['jp', 0, 0],