Browse Source

fixed I18N handling of special param values and broken message tags

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
5d6bad5faa
  1. 23
      framework/yii/i18n/I18N.php
  2. 2
      tests/unit/framework/i18n/GettextMessageSourceTest.php
  3. 20
      tests/unit/framework/i18n/I18NTest.php

23
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;
}
}

2
tests/unit/framework/i18n/GettextMessageSourceTest.php

@ -12,6 +12,6 @@ class GettextMessageSourceTest extends TestCase
{
public function testLoadMessages()
{
$this->markTestSkipped();
$this->markTestIncomplete();
}
}

20
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;
}
Loading…
Cancel
Save