In this post, I will show you my workflow for internationalization of Yii based projects.

We will configure sane paths, logically dividing the frontend and backend. We will also use the yii cli tool to generate the translation files for us. Let’s get started.

Create messages directory inside the common directory. Create a file called i18n.php inside common/config directory. Paste the following block of code inside i18n.php.

<?php return [ 'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR, 'languages' => ['pt-PT'], //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' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'messages', 'overwrite' => true, ]; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?php return [ 'sourcePath' = > __DIR__ . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR , 'languages' = > [ 'pt-PT' ] , //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' = > __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'messages' , 'overwrite' = > true , ] ; Make sure to add all required languages to ‘languages’ array. In the above example I have added Portuguese. Add the i18n component to your common/main.php configuration as follows:

'components' => [ ... 'i18n' => [ 'translations' => [ 'frontend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], 'backend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], ], ], ... ], 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 'components' = > [ . . . 'i18n' = > [ 'translations' = > [ 'frontend*' = > [ 'class' = > 'yii\i18n\PhpMessageSource' , 'basePath' = > '@common/messages' , ] , 'backend*' = > [ 'class' = > 'yii\i18n\PhpMessageSource' , 'basePath' = > '@common/messages' , ] , ] , ] , . . . ] , You can now: Set the language in common configuration i.e. 'language' = > 'pt-PT' inside common/main.php .

inside . Set the language at runtime i.e. Yii : : $ app - > language = 'pt-PT'

See the Yii 2.0 Guide on Internationalization for more details on setting a language. In your frontend and backend code, use the following method to create i18n supported strings respectively.

Yii::t('frontend', 'Translatable String'); 1 Yii : : t ( 'frontend' , 'Translatable String' ) ;

Yii::t('backend', 'Translatable String'); 1 Yii : : t ( 'backend' , 'Translatable String' ) ; Run yii message/extract @common/config/i18n.php to generate the translation files inside common/messages. Yii message will generate the translation files as follows:

common/ ... messages/ pt-PT/ backend.php frontend.php ... 1 2 3 4 5 6 7 common/ ... messages/ pt-PT/ backend.php frontend.php ... The files frontend.php and backend.php are now ready to edit the Portuguese translations 🙂

There you have it! Once set up correctly, just run step #7 every time you add new translatable strings to your app. Yii CLI tool automatically and safely merges existing translations with the new strings and you can translate the new strings in the generated files.

Further Reading

http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html

http://www.yiiframework.com/doc-2.0/yii-baseyii.html#t()-detail

Bonus Tip

You can further organize the translatable files i.e. Yii::t('frontend/post', 'Translatable String'); will generate common/messages/pt-PT/frontend/post.php with all translatable strings inside.