From 5d6bad5faab6087e4feff014ad3783cafe368028 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 16 Oct 2013 20:59:54 +0200 Subject: [PATCH] fixed I18N handling of special param values and broken message tags --- framework/yii/i18n/I18N.php | 23 +++++++++++----------- .../framework/i18n/GettextMessageSourceTest.php | 2 +- tests/unit/framework/i18n/I18NTest.php | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/framework/yii/i18n/I18N.php b/framework/yii/i18n/I18N.php index 3c32609..625ba5c 100644 --- a/framework/yii/i18n/I18N.php +++ b/framework/yii/i18n/I18N.php @@ -10,7 +10,6 @@ namespace yii\i18n; use Yii; use yii\base\Component; use yii\base\InvalidConfigException; -use yii\base\InvalidParamException; use yii\log\Logger; /** @@ -73,23 +72,25 @@ class I18N extends Component public function translate($category, $message, $params, $language) { $message = $this->getMessageSource($category)->translate($category, $message, $language); - if (empty($params)) { + + $params = (array)$params; + if ($params === array()) { return $message; } - $params = (array)$params; if (class_exists('MessageFormatter', false) && preg_match('~{\s*[\d\w]+\s*,~u', $message)) { $formatter = new MessageFormatter($language, $message); if ($formatter === null) { - \Yii::$app->getLog()->log("$language message from category $category is invalid. Message is: $message.", Logger::LEVEL_WARNING, 'application'); + Yii::warning("$language message from category $category is invalid. Message is: $message."); + return $message; + } + $result = $formatter->format($params); + if ($result === false) { + $errorMessage = $formatter->getErrorMessage(); + Yii::warning("$language message from category $category failed with error: $errorMessage. Message is: $message."); + return $message; } else { - $result = $formatter->format($params); - if ($result === false) { - $errorMessage = $formatter->getErrorMessage(); - \Yii::$app->getLog()->log("$language message from category $category failed with error: $errorMessage. Message is: $message.", Logger::LEVEL_WARNING, 'application'); - } else { - return $result; - } + return $result; } } diff --git a/tests/unit/framework/i18n/GettextMessageSourceTest.php b/tests/unit/framework/i18n/GettextMessageSourceTest.php index d039629..5c10ff2 100644 --- a/tests/unit/framework/i18n/GettextMessageSourceTest.php +++ b/tests/unit/framework/i18n/GettextMessageSourceTest.php @@ -12,6 +12,6 @@ class GettextMessageSourceTest extends TestCase { public function testLoadMessages() { - $this->markTestSkipped(); + $this->markTestIncomplete(); } } diff --git a/tests/unit/framework/i18n/I18NTest.php b/tests/unit/framework/i18n/I18NTest.php index e46b8cd..ac19c02 100644 --- a/tests/unit/framework/i18n/I18NTest.php +++ b/tests/unit/framework/i18n/I18NTest.php @@ -7,6 +7,7 @@ namespace yiiunit\framework\i18n; +use yii\base\Model; use yii\i18n\I18N; use yii\i18n\MessageFormatter; use yii\i18n\PhpMessageSource; @@ -62,4 +63,23 @@ class I18NTest extends TestCase $this->assertEquals('Er heißt DA VINCI und ist 42 km/h schnell.', $this->i18n->translate('test', $msg, $params, 'de_DE')); } + public function testSpecialParams() + { + $msg = 'His speed is about {0} km/h.'; + + $this->assertEquals('His speed is about 0 km/h.', $this->i18n->translate('test', $msg, 0, 'en_US')); + $this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, 42, 'en_US')); + $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, null, 'en_US')); + $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, array(), 'en_US')); + + $msg = 'His name is {name} and he is {age} years old.'; + $model = new ParamModel(); + $this->assertEquals('His name is peer and he is 5 years old.', $this->i18n->translate('test', $msg, $model, 'en_US')); + } +} + +class ParamModel extends Model +{ + public $name = 'peer'; + public $age = 5; } \ No newline at end of file