Commit 5d6bad5f by Carsten Brandt

fixed I18N handling of special param values and broken message tags

parent bbcee326
...@@ -10,7 +10,6 @@ namespace yii\i18n; ...@@ -10,7 +10,6 @@ namespace yii\i18n;
use Yii; use Yii;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\log\Logger; use yii\log\Logger;
/** /**
...@@ -73,25 +72,27 @@ class I18N extends Component ...@@ -73,25 +72,27 @@ class I18N extends Component
public function translate($category, $message, $params, $language) public function translate($category, $message, $params, $language)
{ {
$message = $this->getMessageSource($category)->translate($category, $message, $language); $message = $this->getMessageSource($category)->translate($category, $message, $language);
if (empty($params)) {
$params = (array)$params;
if ($params === array()) {
return $message; return $message;
} }
$params = (array)$params;
if (class_exists('MessageFormatter', false) && preg_match('~{\s*[\d\w]+\s*,~u', $message)) { if (class_exists('MessageFormatter', false) && preg_match('~{\s*[\d\w]+\s*,~u', $message)) {
$formatter = new MessageFormatter($language, $message); $formatter = new MessageFormatter($language, $message);
if ($formatter === null) { 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.");
} else { return $message;
}
$result = $formatter->format($params); $result = $formatter->format($params);
if ($result === false) { if ($result === false) {
$errorMessage = $formatter->getErrorMessage(); $errorMessage = $formatter->getErrorMessage();
\Yii::$app->getLog()->log("$language message from category $category failed with error: $errorMessage. Message is: $message.", Logger::LEVEL_WARNING, 'application'); Yii::warning("$language message from category $category failed with error: $errorMessage. Message is: $message.");
return $message;
} else { } else {
return $result; return $result;
} }
} }
}
$p = array(); $p = array();
foreach($params as $name => $value) { foreach($params as $name => $value) {
......
...@@ -12,6 +12,6 @@ class GettextMessageSourceTest extends TestCase ...@@ -12,6 +12,6 @@ class GettextMessageSourceTest extends TestCase
{ {
public function testLoadMessages() public function testLoadMessages()
{ {
$this->markTestSkipped(); $this->markTestIncomplete();
} }
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yiiunit\framework\i18n; namespace yiiunit\framework\i18n;
use yii\base\Model;
use yii\i18n\I18N; use yii\i18n\I18N;
use yii\i18n\MessageFormatter; use yii\i18n\MessageFormatter;
use yii\i18n\PhpMessageSource; use yii\i18n\PhpMessageSource;
...@@ -62,4 +63,23 @@ class I18NTest extends TestCase ...@@ -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')); $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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment