mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-15 06:40:59 +08:00
Docs about handling missing translations added
This commit is contained in:
@ -417,6 +417,52 @@ Sometimes you want to correct default framework message translation for your app
|
|||||||
|
|
||||||
Now you can place your adjusted translations to `/path/to/my/message/files`.
|
Now you can place your adjusted translations to `/path/to/my/message/files`.
|
||||||
|
|
||||||
|
### Handling missing translations
|
||||||
|
|
||||||
|
If the translation is missing at the source, Yii displays the requested message content. Such behavior very convenient
|
||||||
|
in case your raw message is a valid verbose text. However, sometimes it is not enough.
|
||||||
|
You may need to perform some custom processing of the situation, when requested translation is missing at the source.
|
||||||
|
This can be achieved via 'missingTranslation' event of the [[yii\i18n\MessageSource]].
|
||||||
|
|
||||||
|
For example: lets mark all missing translations with something notable, so they can be easily found at the page.
|
||||||
|
First we need to setup event handler, this can be done via configuration:
|
||||||
|
|
||||||
|
```php
|
||||||
|
'components' => [
|
||||||
|
// ...
|
||||||
|
'i18n' => [
|
||||||
|
'translations' => [
|
||||||
|
'app*' => [
|
||||||
|
'class' => 'yii\i18n\PhpMessageSource',
|
||||||
|
'fileMap' => [
|
||||||
|
'app' => 'app.php',
|
||||||
|
'app/error' => 'error.php',
|
||||||
|
],
|
||||||
|
'on missingTranslation' => ['TranslationEventHandler', 'handleMissingTranslation']
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
```
|
||||||
|
|
||||||
|
Now we need to implement own handler:
|
||||||
|
|
||||||
|
```php
|
||||||
|
use yii\i18n\MissingTranslationEvent;
|
||||||
|
|
||||||
|
class TranslationEventHandler
|
||||||
|
{
|
||||||
|
public static function(MissingTranslationEvent $event) {
|
||||||
|
$event->translatedMessage = "@MISSING: {$event->category}.{$event->message} FOR LANGUAGE {$event->language} @";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If [[yii\i18n\MissingTranslationEvent::translatedMessage]] is set by event handler it will be displayed as translation result.
|
||||||
|
|
||||||
|
> Attention: each message source handles its missing translations separately. If you are using several message sources
|
||||||
|
and wish them treat missing translation in the same way, you should assign corresponding event handler to each of them.
|
||||||
|
|
||||||
Views
|
Views
|
||||||
-----
|
-----
|
||||||
|
|
||||||
@ -444,7 +490,7 @@ return [
|
|||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
After cofiguring the component can be accessed as `Yii::$app->formatter`.
|
After configuring the component can be accessed as `Yii::$app->formatter`.
|
||||||
|
|
||||||
Note that in order to use i18n formatter you need to install and enable
|
Note that in order to use i18n formatter you need to install and enable
|
||||||
[intl](http://www.php.net/manual/en/intro.intl.php) PHP extension.
|
[intl](http://www.php.net/manual/en/intro.intl.php) PHP extension.
|
||||||
|
Reference in New Issue
Block a user