Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override translation path of module on yii2

Tags:

php

yii

yii2

Suppose I installed module Foo form a repository with composer. The module structure is like this:

- Foo
 |- models
 |- controllers
 |- views
 |- messages
 |- config

Messages folder of Foo contains translation files of module. Now I want override some translation strings of Foo. From Yii2 i18n Documentation I tried to use fileMap property on configuration of translation component to map bar category to bar.php (instead of reading from app\modules\Foo\messages), but it does not have any effect on translations. My i18n component configuration is:

'i18n' => [
    'translations' => [
        '*' => [
            'class' => 'yii\i18n\PhpMessageSource',
            'fileMap' => [
                'bar' => 'bar.php'
            ],
        ],
    ],
],

How do i achieve my goal?

like image 861
meysam Avatar asked Dec 18 '15 13:12

meysam


1 Answers

If you are wanting to have translations for each module contained within the module, then you need to register the translations for that module. It can't be done simply from the config file. You probably already have this in your module file,, I just include for completeness. The code is copied from the documentation, and needs to be in your module file, so in app/modules/Foo.php

<?php

namespace app\modules\foo;

use Yii;

class Module extends \yii\base\Module
{
    public $controllerNamespace = 'app\modules\foo\controllers';

    public function init()
    {
        parent::init();
    /** Register custom translations for this module **/
        $this->registerTranslations();
    }

    public function registerTranslations()
    {

    /**This registers translations for the Foo module **/
        Yii::$app->i18n->translations['modules/foo/*'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath' => '@app/modules/foo/messages',

    /**Tells yii where to find the translations for validation and form categories **/
            'fileMap' => [
                'modules/foo/validation' => 'validation.php',
                'modules/foo/form' => 'form.php',
                ...
            ],
        ];
    }

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

}

In your case it doesn't look like you need to provide file mapping.You could simply use this format for your files

[[basePath]]/LanguageID/CategoryName.php

Unfortunately I can't seem to find a list of the available categories.

If you then want to override some of the module translations you will need to specify the category to be used, like this in your config file. It specifically overrides the modules/foo/bar category.

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

Your translation file needs to follow a folder structure like that in the translation description, so in the above example it would be

app/messages/ [language code] /modules/foo/bar.php

Otherise, you can use fileMap to map to different locations, like if your bar.php file is in app/messages/[language code]

'fileMap' => [
'modules/foo/bar' => 'bar.php'
]
like image 192
Joe Miller Avatar answered Nov 17 '22 10:11

Joe Miller