Yii2 翻译不起作用
Yii2 translation does not work
我有 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',
],
],
],
],
]
然后我在common/config:
中添加了i18n.php
<?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');
但是翻译失败,显示为menu.login
您只需按照以下步骤操作......
步骤 1: 在 common
目录中,创建 messages
文件夹。
第 2 步: 在 common/config
目录中创建 i18n.php
文件,内容如下:
<?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,
];
注意: 确保将所有必需的语言添加到 'languages' 数组。在上面的示例中,我为 Generate Yii2 Framework multi language 添加了英语和俄语。
第三步:在config
文件中添加i18n
组件common/main.php
配置如下:
'components' => [
...
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
...
],
第 4 步:
在公共配置文件中添加语言模块以在您的应用上使用默认语言,例如:
'language' => 'en-EN'
里面 common/main.php
.
您现在可以在任何运行时使用 Yii::$app->language = ‘en-EN’
,例如 URL 请求、查询代码。
注意:在Gii生成的任何模型,控制器中,您可以看到启用I18n票证选项,只需为多语言启用此选项。由于 frontent
或 backend
文件夹:
,Gii 工具将自动生成一个预定义的模型,如下所示
Yii::t('frontend', 'Translatable String');
Yii::t('backend', 'Translatable String');
第 5 步: 运行 来自 Yii2 应用程序文件夹的命令行:
yii message/extract @common/config/i18n.php
此命令行会在common/messages
内生成Yii2 Framework多语言翻译文件,并分为frontend
和backend
文件夹。
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
文件并编辑。
我有 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',
],
],
],
],
]
然后我在common/config:
i18n.php
<?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');
但是翻译失败,显示为menu.login
您只需按照以下步骤操作......
步骤 1: 在 common
目录中,创建 messages
文件夹。
第 2 步: 在 common/config
目录中创建 i18n.php
文件,内容如下:
<?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,
];
注意: 确保将所有必需的语言添加到 'languages' 数组。在上面的示例中,我为 Generate Yii2 Framework multi language 添加了英语和俄语。
第三步:在config
文件中添加i18n
组件common/main.php
配置如下:
'components' => [
...
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
...
],
第 4 步:
在公共配置文件中添加语言模块以在您的应用上使用默认语言,例如:
'language' => 'en-EN'
里面 common/main.php
.
您现在可以在任何运行时使用 Yii::$app->language = ‘en-EN’
,例如 URL 请求、查询代码。
注意:在Gii生成的任何模型,控制器中,您可以看到启用I18n票证选项,只需为多语言启用此选项。由于 frontent
或 backend
文件夹:
Yii::t('frontend', 'Translatable String');
Yii::t('backend', 'Translatable String');
第 5 步: 运行 来自 Yii2 应用程序文件夹的命令行:
yii message/extract @common/config/i18n.php
此命令行会在common/messages
内生成Yii2 Framework多语言翻译文件,并分为frontend
和backend
文件夹。
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
文件并编辑。