Roel
Roel

Reputation: 764

i18n and modules retrieves from wrong path in Yii2

I don't know if this is a bug or a mistake on my end but basically I followed the Yii2 documentation to setup i18n translations for modules. The following snippet is directly copy pasted from the Yii2 guide.

public function init()
{
    parent::init();

    $this->registerTranslations();
}

public function registerTranslations()
{
    Yii::$app->i18n->translations['modules/users/*'] = [
        'class' => 'yii\i18n\PhpMessageSource',
        'sourceLanguage' => 'en',
        'basePath' => '@app/modules/users/messages',
    ];
}

public static function t($category, $message, $params = [], $language = null)
{
    return Yii::t('modules/users/' . $category, $message, $params, $language);
}

According to the guide I should call it like this:

Module::t('validation', 'your custom validation message')

However, Yii2 tries to load the the 'validation.php' from the wrong location. This is the output of the debugger:

The message file for category 'modules/users/validation' does not exist: /Applications/MAMP/htdocs/.../domains/localhost/public_html/.../backend/modules/users/messages/en/modules/users/validation.php

From what I understand, it should be looking for modules/users/message/<lang>/validation.php instead, which makes a lot more sense than what it is looking for right now.

What am I doing wrong?

Thank you in advance.

Upvotes: 1

Views: 1449

Answers (1)

soju
soju

Reputation: 25312

You should simply add a filemap param, e.g. :

public function registerTranslations()
{
    Yii::$app->i18n->translations['modules/users/*'] = [
        'class' => 'yii\i18n\PhpMessageSource',
        'sourceLanguage' => 'en',
        'basePath' => '@app/modules/users/messages',
        'fileMap' => [
            'modules/users/validation' => 'validation.php',
        ],
    ];
}

Read more : http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html#translating-module-messages

EDIT : As stated in Yii2 guide, if you want to remove filemap, your validation.php file should be in modules/users/messages/[lang]/modules/users/validation.php.

Instead of configuring fileMap you can rely on convention which is to use the category name as the file name (e.g. category app/error will result in the file name app/error.php under the basePath.

Upvotes: 2

Related Questions