我的yii2扩展如何使用此扩展的消息?

时间:2018-02-21 14:37:47

标签: yii2 internationalization messages yii-extensions

我的yii2扩展程序如何使用此扩展程序的邮件?

在我作为belove使用的模块上,但在作为扩展程序执行后,它不起作用

public function init()
{
    parent::init();
    $this->registerTranslations();
}


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

public static function t($category, $message, $params = [], $language = null)
{
    return Yii::t('vendor/abdualiym/yii2-contacts/' . $category, $message, $params, $language);
}

```

1 个答案:

答案 0 :(得分:0)

要在扩展程序中拥有自己的消息,我确实遵循:

  • 添加到公共配置文件中: 'contact' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@vendor/abdualiym/yii2-contacts/messages', 'sourceLanguage' => 'en', 'fileMap' => [ 'contact' => 'contact.php', ], ],

然后我用

Yii::t('contact', 'Text')