Commit c229ba72 by Carsten Brandt

fixed a bug in MessageSource and missingtranslation event

fixes #2519: MessageSource removed translation messages when event handler was bound to `missingTranslation`-event
parent 3a6b9341
......@@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Bug #2324: Fixed QueryBuilder bug when building a query with "query" option (mintao)
- Bug #2399: Fixed the bug that AssetBundle did not handle relative URLs correctly (qiangxue)
- Bug #2502: Unclear error message when `$_SERVER['DOCUMENT_ROOT']` is empty (samdark)
- Bug #2519: MessageSource removed translation messages when event handler was bound to `missingTranslation`-event (cebe)
- Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark)
- Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark)
- Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
......
......@@ -91,12 +91,13 @@ class MessageSource extends Component
/**
* Translates the specified message.
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered
* and the original message will be returned.
* @param string $category the category that the message belongs to
* @param string $message the message to be translated
* @param string $language the target language
* @return string|boolean the translated message or false if translation wasn't found
* If the message is not found, a [[EVENT_MISSING_TRANSLATION|missingTranslation]] event will be triggered.
* If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
* If no fallback translation is provided this method will return `false`.
* @param string $category the category that the message belongs to.
* @param string $message the message to be translated.
* @param string $language the target language.
* @return string|boolean the translated message or false if translation wasn't found.
*/
protected function translateMessage($category, $message, $language)
{
......@@ -113,7 +114,9 @@ class MessageSource extends Component
'language' => $language,
]);
$this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
$this->_messages[$key] = $event->message;
if ($event->translatedMessage !== false) {
return $this->_messages[$key][$message] = $event->translatedMessage;
}
}
return false;
}
......
......@@ -18,11 +18,17 @@ use yii\base\Event;
class MissingTranslationEvent extends Event
{
/**
* @var string the message to be translated. An event handler may overwrite this property
* with a translated version if possible.
* @var string the message to be translated. An event handler may use this to provide a fallback translation
* and set [[translatedMessage]] if possible.
*/
public $message;
/**
* @var string|boolean the translated message. An event handler may overwrite this property
* with a translated version of [[message]] if possible. Defaults to false meaning no translation
* is available.
*/
public $translatedMessage = false;
/**
* @var string the category that the message belongs to
*/
public $category;
......
......@@ -7,6 +7,7 @@
namespace yiiunit\framework\i18n;
use yii\base\Event;
use yii\base\Model;
use yii\i18n\I18N;
use yii\i18n\PhpMessageSource;
......@@ -98,6 +99,33 @@ class I18NTest extends TestCase
{
$this->assertEquals('1 item', $this->i18n->translate('test', '{0, number} {0, plural, one{item} other{items}}', 1, 'hu'));
}
/**
* https://github.com/yiisoft/yii2/issues/2519
*/
public function testMissingTranslationEvent()
{
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function($event) {});
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Missing translation message.', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
Event::on(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION, function($event) {
if ($event->message == 'Missing translation message.') {
$event->translatedMessage = 'TRANSLATION MISSING HERE!';
}
});
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
$this->assertEquals('Another missing translation message.', $this->i18n->translate('test', 'Another missing translation message.', [], 'de-DE'));
$this->assertEquals('TRANSLATION MISSING HERE!', $this->i18n->translate('test', 'Missing translation message.', [], 'de-DE'));
$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
Event::off(PhpMessageSource::className(), PhpMessageSource::EVENT_MISSING_TRANSLATION);
}
}
class ParamModel extends Model
......
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