duanfu6160
2016-04-15 14:44
浏览 128
已采纳

Yii2翻译在错误的文件夹中查找

I'm using the Yii2 advanced template. I have implemented the translations(i18n) following this tutorial and reviewing this SO question. YES, I read the documentation.

My translations are not working and I found out in the debugger that it's looking for he translations in the frontend folder instead of he common folder where the message/extract created the translations files:

    The message file for category 'app' does not exist: localhost/frontend/messages/es/app.php

I know the easiest thing would be to move the messages folder to the frontend folder since I'm not using translations in the backend, but I'd like to understand what I'm doing wrong.

This is my i18n file located in common/config:

'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR,
'languages' => ['es'], //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,

This is my common/config/main file

'i18n' => [
        'translations' => [
            'frontend*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
            ],
            'backend*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
            ],
        ],
    ],

This is where the aliases are defined (default. common/config/bootstrap) and echoing @common returns common:

Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmu1914 2016-04-15 15:09
    已采纳

    Your code is correct, but from the error message seems that you are calling:

    Yii::t('app', '...');
    

    Instead in your common/config/main you have declared entries for 'frontend*' and 'backend*', but not for 'app*'. So Yii will continue search inside frontend repository folder.

    The common/config/main should contain (if you want to use Yii::t('app',...') ):

                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@common/messages',
                ],
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongsu2807 2016-04-15 15:22

    Using your configuration, the translations must be called using:

    Yii::t('frontend','Frontend_string');
    

    for the frontend and,

    Yii::t('backend','Backend_string');
    

    for the backend content.

    See the point 6. in the tutorial

    评论
    解决 无用
    打赏 举报