Not working Translations with Environments in Yii2
php, yii, yii2
Solution
The problem is in app*, because it's not app* category, this works:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'fileMap' => [
'companie' => 'companie.php',
],
],
],
],
Or if you want write `'companie*' =>`
If it is still not working, you did set incorrect path to translate files. By default it must be `BasePath/messages/LanguageID/CategoryName.php`.
If you want to use one file in backend and frontend you should create for example common alias in common config (advanced yii application) and set this alias in i18n config. This is full example:
Common config:
Yii::setAlias('@common', dirname(__DIR__));
return [
'language' => 'ru',
'sourceLanguage' => 'ru',
'components' => [
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
'fileMap' => [
'companie' => 'companie.php',
],
....
In traslate file /common/messages/en-US/companie.php
<?php
return [
'string in russian' => 'string in english'
];
Check translate using this code:
\Yii::$app->language = 'en-US';
echo \Yii::t('companie', 'string in russian');
Problem
I have set `3 environments`. My app needs to load different sets of translations because each env is different. I have the `RO`, `HU`, `DE` languages. I am trying to set the translations, but it does not work. in frontend/config main.php i have: `'sourceLanguage' => 'en', 'language' => 'en',` in the `frontend/web/index.php` i have: `defined('YII_ENV') or define('YII_ENV', 'dev_ro');` also, i am merging the config array: `(file_exists(__DIR__ . '/../../environments/' . YII_ENV . '/common/config/main-local.php') ? require(__DIR__ . '/../../environments/' . YII_ENV . '/common/config/main-local.php') : [])` now, in `environments/dev_ro/common/config/`, in `components` i have: ``` 'i18n' => [ 'translations' => [ 'companie' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@app/messages', 'sourceLanguage' => 'en', 'fileMap' => [ 'companie' => 'companie.php', ], ], ], ], ``` in the `Companie` model i have: ``` 'nume' => Yii::t('companie', 'Name'), ``` this is the movie, with my thing: movie