【问题标题】:Yii2 Translations looking in wrong folderYii2 Translations 在错误的文件夹中查找
【发布时间】:2016-04-15 14:44:45
【问题描述】:

我正在使用 Yii2 高级模板。我已经在this tutorial 和审查this SO question 之后实施了翻译(i18n)。是的,我阅读了文档。

我的翻译不起作用,我在调试器中发现它正在前端文件夹中寻找他的翻译,而不是消息/提取创建翻译文件的公共文件夹:

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

我知道最简单的方法是将消息文件夹移动到前端文件夹,因为我没有在后端使用翻译,但我想了解我做错了什么。

这是我位于 common/config 中的 i18n 文件:

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

这是我的 common/config/main 文件

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

这是定义别名的地方(默认。common/config/bootstrap)并且回显@common 返回 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');

【问题讨论】:

    标签: php internationalization yii2 yii2-advanced-app


    【解决方案1】:

    您的代码是正确的,但从错误消息看来您正在调用:

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

    相反,在您的 common/config/main 中,您已经声明了 'frontend*' 和 'backend*' 的条目,但没有声明 'app*' 的条目。所以 Yii 将继续在前端存储库文件夹中搜索。

    common/config/main 应该包含(如果你想使用Yii::t('app',...')):

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

    【讨论】:

    • 这成功了!谢谢!我误解了配置的那部分,但现在我明白了。再次感谢。
    【解决方案2】:

    使用您的配置,必须使用以下方法调用翻译:

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

    对于前端和,

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

    用于后端内容。

    tutorial中的第6点

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2016-10-28
      相关资源
      最近更新 更多