Module translations
As separate entities, modules should contain their own message files separate from your application message files. The recommended way to use messages within modules is as follows:
- Within the
init()
method of your module, define a new translation section for your module:parent::init();Yii::$app->i18n->translations['modules/mymodule*'] = [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@app/modules/mymodule/messages' ];
- Create a static method wrapper for
Yii::t()
:public static function t($category, $message, $params = [], $language = null) { return Yii::t('modules/mymodule/' . $category, $message, $params, $language); }
- Finally, create a separate message configuration file within the
messages/
directory of your module that specifies the translator to be<ModuleName>::t
:<?php return [ 'sourcePath' =>...