php - Yii2 翻译查找错误的文件夹

标签 php internationalization yii2 yii2-advanced-app

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

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

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

这是定义别名的地方(default.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');

最佳答案

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

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

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

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

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

关于php - Yii2 翻译查找错误的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650275/

相关文章:

php - 按引用代码对图像进行分组

php - 如何使用 PHP 从 $_SERVER ['HTTP_ACCEPT_LANGUAGE' ] 获取语言值?

php - 使用 PHP 和 MySQL 存储和显示 unicode 字符串 (हिन्दी)

yii2 - Yii jui 日期选择器格式

javascript - Yii2 POST方法不显示 View

javascript文件中的php常量

php - Zend Framework 2 getControllerConfig 与 getConfig

php - 设置 session 变量有延迟吗?仅在第一次尝试时失败

file-upload - 让用户上传和存储文件名中包含国家字符的文件的想法有多糟糕?

php - Yii2自定义errorHandler组件