Translations in sourceLanguage do not work in Yii2 application

I use keyword-based translations in my Yii2 application (I know this is not the best option, but I have no others). I prepared the files @app/messages/pl/app.php and @app/messages/en/app.php with translation strings using keywords and not full-featured sentences or words:

 <?php return [ 'name_english'=>'Name in English', 'keywords_english'=>'Keywords in English' ]; ?> 

I installed my application to use the default Polish language:

 'language' => 'pl', 'sourceLanguage' => 'en', 

And I invoke the translation:

 Yii::t('app', 'keywords_english'); 

Everything works fine when the language is actually installed on the base, Polish ( pl ):

enter image description here

But, when I change it to English ( en ; either by setting Yii::$app->language at runtime, or by changing the configuration of the application), the translation fails, and I get keywords_english :

enter image description here

I put die() at the beginning of the @app/messages/pl/app.php and @app/messages/en/app.php , and I clearly see that when the language is set to English, the second file does not include Yii2 (launch application follows), whereas when the language is Polish, the first file is included, and the application stream is split into die() .

What am I missing? Why doesn't Yii2 use translations from the @app/messages/en/app.php if the language is set to English ( en )?

EDIT . By default, I did not change the configuration of the default i18n component in my application configuration, since I did not need this. Translation files are stored in the default position ( @app/messages/<language>/ ) and use the default class ( yii\i18n\PhpMessageSource ). And this works for all languages ​​except sourceLanguage . At some point, I tried to change the configuration:

 'i18n' => [ 'translations' => [ '*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en', 'basePath' => '@app/messages' ], ], ], 

But that did not change (why it should - it still uses the default settings).

+5
source share
2 answers

According to samdark at the Yii Forum , this is by design. Translations are not performed if language = sourceLangage .

To get around this and force translations in this case, you need to set forceTranslation to true .

Therefore, you need to add / change the i18n component in the components section of the application configuration as follows:

 'i18n' => [ 'translations' => [ 'app' => [ 'class' => 'yii\i18n\PhpMessageSource', 'forceTranslation' => true ], ], ], 
+6
source

Decision:

 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@app/messages', 'sourceLanguage' => 'en_US', 'fileMap' => [ 'app' => 'app.php', ], ], 

Replies to your comment:

1) 'sourceLanguage' => 'en_US' - you must use the full language. Since English can be en_US , en_UK , etc. The format for the language/locale is ll-CC where ll is a two- or three-letter lowercase code for a language according to ISO-639 and CC is the country code according to ISO-3166. from [doc][1]

2) In the key category of use. And the category set in Yii::t('category'...)

+1
source

All Articles