Override module translation path to yii2

Suppose I installed the Foo module from a repository with a composer. The structure of the module is as follows:

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

Messages folder Foo contains module translation files. Now I want to override some Foo translation lines. From Yii2 i18n Documentation I tried to use the fileMap property to configure the translation component to display the bar category in bar.php (instead of reading from app\modules\Foo\messages ), but this does not affect translations. My i18n component configuration:

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

How do I achieve my goal?

+8
php yii yii2
source share
1 answer

If you want to have translations for each module contained in a module, you need to register translations for this module. This cannot be done from the configuration file. You probably already have this in your module file, I just turned on for completeness. The code is copied from the documentation and should 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 does not look like you need to provide a file association. You can simply use this format for your files.

 [[basePath]]/LanguageID/CategoryName.php 

Sorry, I can’t find a list of available categories.

If you want to override some module translations, you need to specify the category that will be used, for example, in your configuration file. It specifically redefines the module category / foo / bar.

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

Your translation file should follow the folder structure, as in the description of the translation, so in the example above it will be

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

In other words, you can use fileMap for matching in different places, for example, if your bar.php file is in the application / messages / [language code]

 'fileMap' => [ 'modules/foo/bar' => 'bar.php' ] 
+2
source share

All Articles