duanfu6160 2016-04-15 06:44
浏览 131
已采纳

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 07: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',
                ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部