开启配置
'lang_switch_on' => true,
'lang_list' => ['zh-cn','en-us'],
定义语言包
框架或模块目录\lang\zh-cn.php
return [
'CHINESE'=>'中文',
'ENGLISH' =>'英文',
]
框架或模块目录\lang\en-us.php
return [
'中文'=>'CHINESE',
'英文' =>'ENGLISH',
]
控制器中方法
public function changelang() {
$lang=input('lang');
switch ($lang) {
case 'en':
cookie('think_var', 'en-us');
break;
case 'zn':
cookie('think_var', 'zh-cn');
break;
default:
cookie('think_var','zh-cn');
break;
}
}
HTML示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="background: lightskyblue;width: 150px;height: 20px;">
{:lang('CHINESE')}
</div>
<div style="background: forestgreen;width: 150px;height: 20px;">
{:lang('ENGLISH')}
</div>
<br><br><br>
<hr>
<button class="n" lang="zn">中文</button>
<button class="n" lang="en">英文</button>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.js"></script>
<script>
$('.n').click(function(){
var data={'lang':$(this).attr('lang')};
console.log(data.lang);
$.get("{:url('changelang')}",data,function(){
location.reload();
})
})
</script>
</body>
</html>