Yii2 translation does not work

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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

then I added i18n.php in common/config:

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

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

'login',  ];

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

but the translation didn't work, it displayed as menu.login

回答1:

You Just Follow This Steps......

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

Step 2: Create i18n.php file inside common/config directory with following content:

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

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.

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*'         
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!