Yii2翻译不起作用

Yii2翻译不起作用

本文介绍了Yii2翻译不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Yii2高级模板,我想为前端视图设置翻译,这是我所做的:

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' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
                'sourceLanguage' => 'en-US',
                'fileMap' => [
                     'app' => 'app.php',
                     'app/error' => 'error.php',
                 ],
            ],
        ],
     ],
]

然后我在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,
];

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

<?php

return[

    // Menu texts

    'menu.login'=>'login',

];

,我在视图中将其用作:Yii::t('app', 'menu.login');

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

但翻译无效,显示为menu.login

推荐答案

您只需按照以下步骤操作即可……

You Just Follow This Steps......

步骤1:common目录中,创建messages文件夹.

Step 1: In the common directory , create messages folder.

步骤2:common/config目录中创建具有以下内容的i18n.php文件:

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,
];

注意:请确保将所有必需的语言添加到语言"数组中.在上面的示例中,我为Generate Yii2 Framework多语言添加了英语和俄语.

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.

步骤3:如下所示,在config文件common/main.php配置中添加i18n组件:

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',
            ],
        ],
    ],
    ...
],

第4步:

在通用配置文件中添加语言模块,以在您的应用上使用默认语言,例如:

Add the language module in common config file to use the default language on your app, such as:

'language' => 'en-EN'.

您现在可以在任何运行时(例如URL请求,查询代码)使用Yii::$app->language = ‘en-EN’.

You now can use Yii::$app->language = ‘en-EN’ at any runtime like URL request, query code.

注意:在任何模型Gii的Controller Generate中,您都可以看到Enable I18n票证选择,只需为Multi language启用即可.由于frontentbackend文件夹,Gii Tool将自动生成具有以下定义的模型:

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');

步骤5:从Yii2应用文件夹运行以下命令行:

Step 5: Run this command line from Yii2 app folder:

yii message/extract @common/config/i18n.php

此命令行将在common/messages内部生成Yii2 Framework多语言翻译文件,并分为frontendbackend文件夹.

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
 .....

如果要编辑翻译文本,只需打开backend.phpfrontend.php文件并进行编辑.

If you want to edit the translate text, just open backend.php or frontend.php file and edit.

这篇关于Yii2翻译不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:29