Yii2 国际化代码而不是整个句子
Yii2 Internationalization with code instead of a whole sentence
我目前正在为一个使用 Yii2 的网站进行国际化。
通常我是这样使用的:
Yii::t('frontend', 'Do you have something to eat?')
然后,我在 common\messages 中有两个文件夹,分别是 en-EN 和 id-ID。每个里面都有frontend.php。
en-EN/frontend.php
return [
];
id-ID/frontend.php
return [
'Do you have something to eat?' => 'Ada yang bisa dimakan?'
];
还可以,但是我觉得不太实用,因为句子太长,漏一个字不行。
所以我尝试使用这样的东西:
Yii::t('frontend', 'My.Random.Sentence');
en-EN/frontend.php
return [
'My.Random.Sentence' => 'Do you have something to eat?',
];
id-ID/frontend.php
return [
'My.Random.Sentence' => 'Ada yang bisa dimakan?'
];
它只对id-ID有效,但是当我改回英文时,它仍然显示My.Random.Sentence而不是Do you have something to eat.
这是我在i18n.php
中的配置
return [
'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['en-EN','id-ID'],
'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,
];
这是在frontend/config/main.php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = 'en-US';
return [
// other options
'language' => $_SESSION['lang'],
'sourceLanguage' => 'en-EN',
'components' => [
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
],
//other options
];
这不可能吗?解决我的问题的正确方法是什么?
默认情况下,当 $sourceLanguage
和 $language
相同时,i18n
组件不会翻译字符串。相反,它 returns 源字符串。
但幸运的是,即使 $sourceLanguage
和 $language
属性相同,您也可以强制它翻译字符串。为此,您需要将 $forceTranslation
属性 of MessageSource
设置为 true。你可以在 frontend/config/main.php 中这样做:
return [
// other options
'components' => [
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
'forceTranslation' => true,
],
// ...
],
],
],
//other options
];
我目前正在为一个使用 Yii2 的网站进行国际化。
通常我是这样使用的:
Yii::t('frontend', 'Do you have something to eat?')
然后,我在 common\messages 中有两个文件夹,分别是 en-EN 和 id-ID。每个里面都有frontend.php。
en-EN/frontend.php
return [
];
id-ID/frontend.php
return [
'Do you have something to eat?' => 'Ada yang bisa dimakan?'
];
还可以,但是我觉得不太实用,因为句子太长,漏一个字不行。
所以我尝试使用这样的东西:
Yii::t('frontend', 'My.Random.Sentence');
en-EN/frontend.php
return [
'My.Random.Sentence' => 'Do you have something to eat?',
];
id-ID/frontend.php
return [
'My.Random.Sentence' => 'Ada yang bisa dimakan?'
];
它只对id-ID有效,但是当我改回英文时,它仍然显示My.Random.Sentence而不是Do you have something to eat.
这是我在i18n.php
中的配置return [
'sourcePath' => __DIR__. '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['en-EN','id-ID'],
'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,
];
这是在frontend/config/main.php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = 'en-US';
return [
// other options
'language' => $_SESSION['lang'],
'sourceLanguage' => 'en-EN',
'components' => [
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
'backend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
],
],
],
],
//other options
];
这不可能吗?解决我的问题的正确方法是什么?
默认情况下,当 $sourceLanguage
和 $language
相同时,i18n
组件不会翻译字符串。相反,它 returns 源字符串。
但幸运的是,即使 $sourceLanguage
和 $language
属性相同,您也可以强制它翻译字符串。为此,您需要将 $forceTranslation
属性 of MessageSource
设置为 true。你可以在 frontend/config/main.php 中这样做:
return [
// other options
'components' => [
'i18n' => [
'translations' => [
'frontend*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/messages',
'forceTranslation' => true,
],
// ...
],
],
],
//other options
];