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

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

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型