You just follow these steps ......
Step 1: In the common directory, create the messages folder.
Step 2: Create an i18n.php file inside the common/config directory with the following contents:
<?php return [ 'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR, 'languages' => ['en-EN', 'ru-RU'], //Add languages to the array for the language files to be generated, here are English and Russian. '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', //path of messages folder created above 'overwrite' => true, ];
Note. Be sure to add all the necessary languages ββto the 'languages' array. In the above example, I added English and Russian to create the multilingual language Yii2 Framework.
Step 3: Add the i18n component to the config configuration of the common/main.php file as follows:
'components' => [ ... 'i18n' => [ 'translations' => [ 'frontend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], 'backend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], ], ], ... ],
Step 4:
Add the language module to the shared configuration file to use the default language in your application, for example:
'language' => 'en-EN' inside common/main.php .
Now you can use Yii::$app->language = 'en-EN' in any runtime, such as request URL, request code.
Note: In any model, Generate by Gii controller, you can see the Enable I18n ticket option, just enable it for the Multi language. The Gii Tool will automatically generate the model predefined below due to the frontent or backend folder:
Yii::t('frontend', 'Translatable String'); Yii::t('backend', 'Translatable String');
Step 5: Run this command line from the Yii2 application folder:
yii message/extract @common/config/i18n.php
This command line will generate translation files for several Yii2 Framework languages ββwithin common/messages and divide it into a frontend and backend folder.
For example: Yii message will generate the translation files as follows: common/ ..... messages/ en-EN/ backend.php frontend.php ru-RU/ backend.php frontend.php .....
If you want to edit the translation text, just open the backend.php or frontend.php file and edit.