Yii 2.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' => ['@'],
],
共 0 条回复
没有找到数据。
唯_爱
注册时间:2017-03-14
最后登录:2022-03-30
在线时长:9小时0分
最后登录:2022-03-30
在线时长:9小时0分
- 粉丝2
- 金钱70
- 威望0
- 积分160