迷失在 yii2 模块 i18n 翻译中

Lost in yii2 module i18n translation

我正在尝试将多语言功能添加到我在俄语中找到的模块,但是当我尝试实现它时遇到问题,可能是配置,不确定。

我像往常一样在应用程序中进行了文本替换:

'department' => Yii::t('app', 'Departament'),

然后我用消息命令生成了翻译文件,到目前为止一切正常,我得到了正确的结构:

vendor/rico/yii2-ticket/messages/en/app.php
vendor/rico/yii2-ticket/messages/es/app.php

然后我在yii2-ticket模块文件的init中添加了一个函数:

public function init() {
    User::$user = ($this->userModel !== false) ? $this->userModel : Yii::$app->user->identityClass;
    parent::init();
    $this->registerTranslations();
}
/**
 * Registration of translation class.
 */
protected function registerTranslations()
{
    Yii::$app->i18n->translations['ricco/ticket'] = [
        'class' => 'yii\i18n\PhpMessageSource',
        'sourceLanguage' => 'en',
        'basePath' => '@ricco/ticket/messages',
        'fileMap' => [
            'ricco/ticket' => 'app.php',
        ],
    ];
}

我在基于高级应用程序的 yii2 应用程序中使用此模块,在配置文件中我有以下内容:

$config = [
'name' => 'London Literary Scouts',
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'extensions' => require(__DIR__ . '/../../vendor/yiisoft/extensions.php'),
'sourceLanguage' => 'en',
'language' => 'en',
'bootstrap' => ['log'],
'modules' => [
    'treemanager' => [
        'class' => '\kartik\tree\Module',
    // other module settings, refer detailed documentation
    ],
    'newsletter' => [
        'class' => 'tikaraj21\newsletter\Newsletter',
    ],
    'comment' => [
        'class' => 'yii2mod\comments\Module',
    ],
    'ticket' => [
        'class' => 'ricco\ticket\Module'
    ],
],

...

    'i18n' => [
        'translations' => [
            'app' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
            ],
            '*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@common/messages',
                'fileMap' => [
                    'common' => 'common.php',
                    'backend' => 'backend.php',
                    'frontend' => 'frontend.php',
                ],
                'on missingTranslation' => ['\backend\modules\i18n\Module', 'missingTranslation']
            ],

运行在开发模式下运行应用程序我在日志中没有收到任何错误。

但是如果我将 en/app.php 文件复制到我的应用程序的 common/messages/en 文件夹中,它就可以工作了。

但我想从模块中获得翻译 运行,以便其他人可以根据常规配置使用它。

已编辑

在尝试并从 csminb 答案中理解之后,我从我的主应用程序中删除了所有 i18n 配置。

1-我将模块硬编码文本中的所有翻译如下:

<?= Yii::t('ticket', 'Go back') ?>

2- 在我的 console/config 文件夹中创建了配置文件 ./yii message/config @app/config/i18n-ricco.php

3- 编辑以添加语言和正确的路径

return [
'color' => null,
'interactive' => true,
'help' => null,
'sourcePath' => '@vendor/ricco/yii2-ticket',
'messagePath' => '@vendor/ricco/yii2-ticket/messages',
'languages' => ['en','ru','es'],
'translator' => 'Yii::t',
'sort' => false,
'overwrite' => true,
'removeUnused' => false,
'markUnused' => true,
'except' => [
    '.svn',
    '.git',
    '.gitignore',
    '.gitkeep',
    '.hgignore',
    '.hgkeep',
    '/messages',
    '/BaseYii.php',
],
'only' => [
    '*.php',
],
'format' => 'php',
'db' => 'db',
'sourceMessageTable' => '{{%source_message}}',
'messageTable' => '{{%message}}',
'catalog' => 'messages',
'ignoreCategories' => [],

];

4-运行消息命令

./yii message/config @app/config/i18n-ricco.php

消息命令生成了包含 ticket.php 个文件的所有语言文件夹。

5-在模块的init函数中添加配置:

    /**
 * @inheritdoc
 */
public function init() {
    User::$user = ($this->userModel !== false) ? $this->userModel : Yii::$app->user->identityClass;
    parent::init();
    $this->registerTranslations();
}
/**
 * Registration of translation class.
 */
protected function registerTranslations()
{
    Yii::$app->i18n->translations['ticket'] = [
        'class' => 'yii\i18n\PhpMessageSource',
        'sourceLanguage' => 'en',
        'basePath' => '@ricco/ticket/messages',
        'fileMap' => [
          'ticket' => 'ticket.php',
        ],
    ];
}

在这里添加我之前的评论

if you want to have separate module translation, i would use a different scope than app for anything that's in that module, and add that scope to the application when your module initializes.

您没有收到错误的最可能原因是您配置中的 *

```

'i18n' => [
    'translations' => [
        'app' => [
            'class' => 'yii\i18n\PhpMessageSource',
            'basePath' => '@common/messages',
        ],
        // you could remove this (at least in your local dev to make sure errors will be thrown if files are not setup)
        '*' => [
            'class' => 'yii\i18n\PhpMessageSource',
            'basePath' => '@common/messages',
            'fileMap' => [
                'common' => 'common.php',
                'backend' => 'backend.php',
                'frontend' => 'frontend.php',
            ],
            'on missingTranslation' => ['\backend\modules\i18n\Module', 'missingTranslation']
     ]
]

```

在您的模块中设置翻译文件后 /common/modules/Ticket/messages/en/ticket.php 确保在模块初始化时添加此消息源

/common/modules/Ticket/Module.php

... 

public function init()
{
    parent::init();
    if (!isset(Yii::$app->i18n->translations['ticket'])) {
        Yii::$app->i18n->translations['ticket'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en',
            'basePath' => '@vendor/rico/ticket/messages'
        ];
    }
}

通过这种方式,只需包含模块,它就会添加特定于模块的翻译,并且不会干扰您的应用配置

编辑:

更新到 @vendor/rico 的路径,(如果您设置了别名,@rico 也有效)
同时,另一件可能引起混淆的事情是您示例中的 fileMap

```

// this is hard to make sense of, :
// it's a category named `rico/ticket` will point to @vendor/rico/ticket/messages/en/app.php 
// are you usinging it like this? Yii:t('rico/ticket', 'sample') 
'fileMap' => [
    'ricco/ticket' => 'app.php',
],

除非翻译文件太大而无法管理,或者它们的名称太冗长 (like this example),否则我看不出有任何理由使用它,标准名称映射要简单得多