Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

php - Yii2 translation does not work

I have Yii2 advanced template, I want to set translation for my frontend views, here is what I did:

frontend/config/main.php:

'sourceLanguage'=>'en-US',
'language'=>'en-US',
'components' => [
'i18n' => [
     'translations' => [
           'app*' => [
                'class' => 'yiii18nPhpMessageSource',
                'basePath' => '@common/messages',
                'sourceLanguage' => 'en-US',
                'fileMap' => [
                     'app' => 'app.php',
                     'app/error' => 'error.php',
                 ],
            ],
        ],
     ],
]

then I added i18n.php in common/config:

<?php
return [
    'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
    'languages' => ['fr-FR','en-US'], //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,
];

and the common/messages/en-US/app.php:

<?php

return[

    // Menu texts

    'menu.login'=>'login',

];

and I used it in the views as : Yii::t('app', 'menu.login');

but the translation didn't work, it displayed as menu.login

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' => 'yiii18nPhpMessageSource',
                'basePath' => '@common/messages',
            ],
            'backend*' => [
                'class' => 'yiii18nPhpMessageSource',
                '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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...