浏览代码

Implementing naive plural selection and a Sprintf message formatter

Jose Lorenzo Rodriguez 11 年之前
父节点
当前提交
89942a6523

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

@@ -0,0 +1,32 @@
+<?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\I18n\Formatter;
+
+use Aura\Intl\FormatterInterface;
+
+/**
+ *
+ */
+class SprintfFormatter implements FormatterInterface {
+
+	public function format($locale, $message, array $vars) {
+		if (isset($vars['_count']) && !is_string($message)) {
+			$message = $message[$vars['_count'] - 1];
+		}
+
+		return vsprintf($message, $vars);
+	}
+
+}

+ 15 - 3
src/I18n/I18n.php

@@ -20,6 +20,7 @@ use Aura\Intl\Package;
 use Aura\Intl\PackageLocator;
 use Aura\Intl\TranslatorFactory;
 use Aura\Intl\TranslatorLocator;
+use Cake\I18n\Formatter\SprintfFormatter;
 
 /**
  * I18n handles translation of Text and time format strings.
@@ -39,7 +40,9 @@ class I18n {
 		$translators = new TranslatorLocator(
 			new PackageLocator,
 			new FormatterLocator([
-				'basic' => function() { return new \Aura\Intl\BasicFormatter; },
+				'basic' => function() {
+					return new SprintfFormatter;
+				},
 				'intl'  => function() { return new \Aura\Intl\IntlFormatter; },
 			]),
 			new TranslatorFactory,
@@ -57,15 +60,24 @@ class I18n {
 			return;
 		}
 
+		$translators = static::translators();
+
 		if ($locale) {
+			$currentLocale = $translators->getLocale();
 			static::translators()->setLocale($locale);
 		}
 
 		try {
-			return static::translators()->get($package);
+			$translator = $translators->get($package);
 		} catch (LoadException $e) {
-			return static::_fallbackTranslator($package, $locale);
+			$translator = static::_fallbackTranslator($package, $locale);
+		}
+
+		if (isset($currentLocale)) {
+			$translators->setLocale($currentLocale);
 		}
+
+		return $translator;
 	}
 
 	protected static function _fallbackTranslator($package, $locale) {

+ 15 - 0
tests/TestCase/I18n/I18nTest.php

@@ -46,4 +46,19 @@ class I18nTest extends TestCase {
 		$translator = I18n::translator('default', 'es_ES');
 		$this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
 	}
+
+/**
+ * Tests that plural rules are correctly used for the english language
+ *
+ * @return void
+ */
+	public function testPluralSelection() {
+		$translator = I18n::translator(); // en_US
+		$result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
+		$this->assertEquals('1 is 1 (po translated)', $result);
+
+		$result = $translator->translate('%d = 0 or > 1', ['_count' => 2]);
+		$this->assertEquals('2 is 2-4 (po translated)', $result);
+	}
+
 }

+ 0 - 1
tests/TestCase/I18n/Parser/MoFileParserTest.php

@@ -67,4 +67,3 @@ class MoFileParserTest extends TestCase {
 	}
 
 }
-