唯_爱 2020-03-06 15:06:14 149次浏览 0条回复 0 0 0

main.php

'language' => 'zh-CN',
'components' => [
      // 语言
        'i18n' => [
        'translations' => [
            'common*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'fileMap' => [
                    'common' => 'common.php',
                ],
            ],
            'goods*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'fileMap' => [
                    'common' => 'goods.php',
                ],
            ],
            'order*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'fileMap' => [
                    'common' => 'order.php',
                    ],
            ],
            'message-template*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'fileMap' => [
                    'common' => 'message-template.php',
                ],
            ],
            'push-log*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'fileMap' => [
                    'common' => 'push-log.php',
                ],
            ],
        ],
    ],
],

在入口文件

$application = new yii\web\Application($config);
$application->language = isset(Yii::$app->session['language']) ? Yii::$app->session['language'] : 'zh-CN';
$application->run();

接着添加切换语言点击

<li class="dropdown tasks-menu">
    <?= Html::a(Yii::t('common', 'CN'), ['/site/messages?zh=zh-CN']) ?>
</li>

<li class="dropdown tasks-menu">
    <?= Html::a(Yii::t('common', 'US'), ['/site/messages?zh=en']) ?>
</li>

然后在对应 Controller 加入方法

public function actionMessages()
{
    $language = Yii::$app->request->get('zh');

    $session = Yii::$app->session;
    $session->open();
    if(isset($language)){
    Yii::$app->session['language'] = $language;
    }
    $this->redirect(Yii::$app->request->headers['Referer']);
}

在上面权限哪里加

[
    'actions' => ['messages'],
    'allow' => true,
    'roles' => ['@'],
],
亿速云
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册