我有Yii2高级模板,我想为前端视图设置翻译,这是我所做的:
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', ], ], ], ], ]
然后我加i18n.php在common/config:
i18n.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:
common/messages/en-US/app.php:
<?php return[ // Menu texts 'menu.login'=>'login', ];
我在视图中使用它为: Yii::t('app', 'menu.login');
Yii::t('app', 'menu.login');
但翻译无效,显示为 menu.login
menu.login
您只需按照以下步骤操作……
步骤1: 在common目录中,创建messages文件夹。
common
messages
步骤2:i18n.php在common/config目录内创建文件,内容如下:
common/config
<?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多语言添加了英语和俄语。
步骤3:i18n在config文件common/main.php配置中添加组件,如下所示:
i18n
config
common/main.php
'components' => [ ... 'i18n' => [ 'translations' => [ 'frontend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], 'backend*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@common/messages', ], ], ], ... ],
第四步:
在通用配置文件中添加语言模块,以在您的应用上使用默认语言,例如:
'language' => 'en-EN'里面common/main.php。
'language' => 'en-EN'
现在,您可以Yii::$app->language = ‘en-EN’在任何运行时使用 ,例如URL请求,查询代码。
Yii::$app->language = ‘en-EN’
注意: 在任何由Gii生成的Controller模型中,您都可以看到Enable I18n票证选择,只需为Multi language启用即可。由于frontent或backend文件夹,Gii Tool将自动生成一个预定义如下的模型:
frontent
backend
Yii::t('frontend', 'Translatable String'); Yii::t('backend', 'Translatable String');
步骤5: 从Yii2应用文件夹运行以下命令行:
yii message/extract @common/config/i18n.php
此命令行将在内部生成Yii2 Framework多语言翻译文件common/messages并分为frontend和backend文件夹。
common/messages
frontend
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.php或frontend.php归档并编辑。
backend.php
frontend.php