yii2 多語言設定
1.設定預設語言:在mail.php設定檔加上:'language'=>'zh_cn'
2.多語言切換
html程式碼:
<a href="<?php echo Yii::$app->urlManager->createUrl(['/test/language','lang'=>'zh_cn']);?>">中文</a> <a href="<?php echo Yii::$app->urlManager->createUrl(['/test/language','lang'=>'en']);?>">English</a>
//语言切换 public function actionLanguage(){ $language= \Yii::$app->request->get('lang'); if(isset($language)){ \Yii::$app->session['language']=$language; } //切换完语言哪来的返回到哪里 $this->goBack(\Yii::$app->request->headers['Referer']); }
4.修改入口檔案web/index.php :
(new yiiwebApplication($config))->run();
改為app
new yiiwebApplication($config);
$application -> language = isset(Yii::$app->session['language']) ? Yii::$app->session['language'] : 'zh_cn';
$application -> run();