yii2 Set the method to switch languages: first configure the components; then create a messages directory in the web directory at the same level, which stores the language configuration file; then initialize each controller; finally write the controller method to implement Just switch the language.
##Yii2.0 realizes multi-language switching
Recommendation: "yii tutorial"
1. Configure components'components' => [ 'i18n' => [ 'translations' => [ '*' => [ 'class' => 'yii\i18n\PhpMessageSource', //'basePath' => '/messages', 'fileMap' => [ 'app' => 'app.php', ], ], ], ], ]
'操作', 'Search' => '搜索', 'Reset' => '重置', ];
$application = new yii\web\Application($config); $application->language = isset(\Yii::$app->session['language']) ? \Yii::$app->session['language'] : 'en'; $application->run();
public function actionLanguage(){ $language= \Yii::$app->request->get('lang'); if(isset($language)){ \Yii::$app->session['language']=$language; } //切换完语言哪来的返回到哪里 $this->goBack(\Yii::$app->request->headers['Referer']); }
The above is the detailed content of How to set language switching in yii2. For more information, please follow other related articles on the PHP Chinese website!