Reputation: 233
I currently have a widget inside a module and I would like to create a messages directory so that I can translate module strings from inside that folder.
The issue I have is that I can't get the widget to get the translations from inside the module folder.
This is my structure
frontend/modules/comments
frontend/modules/comments/Module.php
frontend/modules/comments/widgets/CommentsWidget.php
frontend/modules/comments/widgets/views/_form.php
-Module.php
namespace frontend\modules\comments;
use Yii;
class Module extends \yii\base\Module {
public $controllerNamespace = 'frontend\modules\comments\controllers';
public function init() {
parent::init();
$this->registerTranslations();
}
public function registerTranslations() {
Yii::$app->i18n->translations['modules/comments/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@frontend/modules/comments/messages',
'fileMap' => [
'modules/comments/comments' => 'comments.php',
]
];
}
public static function t($category, $message, $params = [], $language = null) {
return Yii::t('modules/comments/' . $category, $message, $params, $language);
}
}
I use the following code to call the translation.
Module::t('comments', 'COMMENT_REPLY')
But it doesn't seem to work. Any ideas? Thank you in advance!
Upvotes: 3
Views: 869
Reputation: 815
Have same problem. It's because you register translations is in init() method (when instance of module was created). You can change registerTranslations() method to static:
public function init()
{
// ...
self::registerTranslations();
}
public static function registerTranslations()
{
// ...
}
And call it Content::registerTranslations();
in your widget before use Module::t()
.
Upvotes: 1