我Yii2先進的模板,我想設置翻譯爲我的前端的觀點,這裏是我做過什麼: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
要使用基於密鑰的消息文件,請確保您啓用了'forceTranslation',因爲默認語言的應用程序不會自動翻譯消息 –