Yii2 translation does not work
You Just Follow This Steps......
Step 1: In the common
directory , create messages
folder.
Step 2: Create i18n.php
file inside common/config
directory with following content:
<?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: Make sure to add all required languages to 'languages' array. In the above example I have added English and Russian for Generate Yii2 Framework multi language.
Step 3: Add the i18n
component in config
file 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',
],
],
],
...
],
Step 4:
Add the language module in common config file to use the default language on your app, such as:
'language' => 'en-EN'
inside common/main.php
.
You now can use Yii::$app->language = ‘en-EN’
at any runtime like URL request, query code.
Note: In any Model, Controller Generate by Gii, you can see Enable I18n ticket choice, just enable this for Multi language. Gii Tool will auto generate a Model has pre-defined as below, due to frontent
or backend
folder:
Yii::t('frontend', 'Translatable String');
Yii::t('backend', 'Translatable String');
Step 5: Run this command line from Yii2 app folder:
yii message/extract @common/config/i18n.php
This command line will Generate Yii2 Framework multi language translation files inside common/messages
and divide into 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 translate text, just open backend.php
or frontend.php
file and edit.
Yii2 i18n not working
You can set target language in your config:
...
'language' => 'ru-RU',
...
If 'sourceLanguage' => 'en-US',
yii will translate from en-US
to ru-RU
.
And config i18n
component will be:
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@app/translation',
'fileMap' => [
'app' => 'app.php',
],
],
],
],
Directory structure is:
- translation
- ru-RU
- app.php
Example in file app.php
return [
'Home' => 'abcxyz',
'source key' => 'translate to russian',
];
Hope it helpful.
Goodluck and have fun!
Translations in sourceLanguage does not work in Yii2 application
According to samdark at Yii Forum, this is by design. Translations are not performed, if language
= sourceLangage
.
To workaround this, and force translations in this case, one must set forceTranslation
to true
.
Therefore, one must add / modify i18n
component in components
section of application's config in the way similar to this:
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true
],
],
],
Yii2 translation does not work in config/params
Answers:
Because
config/params.php
file will merge with main config before initialization of main application. For translation will used\yii\i18n\I18N
component.Yii2::t()
is not heavy method. But if you have any problems with performance, you can override this method and executeYii:$app->getI18n()->translate()
only for existing strings, or enable cache this values.
Not working Translations with Environments in Yii2
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');
Yii2 Internationalization translation not working for model validation
Issue solved in
https://github.com/yiisoft/yii2/issues/8173
Needed to override pre-defined categories and an issue with Arabic language
Related Topics
Run a Ffmpeg Process in the Background
Send Data from Android to Server via JSON
Remove Trailing Slash from String PHP
Strict Standards: Only Variables Should Be Assigned by Reference PHP 5.4
What Is the Maximum Size of an Array in PHP
PHP File Upload Error Tmp_Name Is Empty
Why PHP Variables Start with a $ Sign Symbol
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP
Get Variables from the Outside, Inside a Function in PHP
How to Serve Documents from Outside the Web Root Using PHP
How to Use String Concatenation to Define a Class Const in PHP
Good Error Handling with File_Get_Contents
Pdo: Invalid Parameter Number: Mixed Named and Positional Parameters
Datetime Class VS. Native PHP Date-Functions
Laravel 3:Looking for Explanation How to Use the Model