Yii2 - Multilingual

I am trying to configure the translation of a site interface using i18l. Here is my i18l.php file located on the / config interface

<?php return [ 'sourcePath' => 'frontend', 'languages' => ['en-US', 'pt-BR'] , //Add languages to the array for the language files to be generated. 'translator' => 'Yii::t', 'sort' => false, 'removeUnused' => false, 'only' => ['*.php'], 'except' => [ '.svn', '.git', '.gitignore', '.gitkeep', '.hgignore', '.hgkeep', '/messages', '/vendor', ], 'format' => 'php', 'messagePath' => 'frontend' . DIRECTORY_SEPARATOR . 'translations', 'overwrite' => true, ]; 

and here is my main.php also on the interface

 (...) 'language' => 'en-US', 'components' => [ 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => 'frontend/translations', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], ], ], ] 

I use <?= Yii::t('app', 'some string') ?> On sites and layouts, and when I run the command. / yii message / extract @ frontend / config / i18n.php, it creates for me a folder called "translations" containing the other two folders "en-US" and "pt-BR" as with app.php, which I already filled some translations. But, nevertheless, the translation does not happen when I change the language to main.php, as it should be (I think). I would appreciate it if someone could help me.

Thanks.

+8
php yii yii2-advanced-app
source share
1 answer

Great post, with all the necessary details.

I struggled with the same things, but you did it pretty well.

So, if you can run the command and it generates a file, then sourcePath correct . If it does not display translation messages at runtime despite changing the settings, I suppose the problem might be on your basePath :

Try the following in your basePath configuration:

 'basePath' => '@frontend/translations', 
+4
source share

All Articles