当前位置:网站首页>ThinkPHP5多语言切换项目实战
ThinkPHP5多语言切换项目实战
2022-06-24 08:08:00 【BigChen_up】
ThinkPHP5多语言切换实战
1.在配置文件中开启多语言配置
2.然后添加多语言目录
这里创建你需要的语言包
在语言包里定义需要翻译的文本,中英文数组的键名写成一致

然后在html文件里输入 {:lang(‘键名’)} 对应的键名。就是下图的写法。
上边都做完以后就可以开始撸代码了:
首先在前端写一个切换语言的AJAX请求PHP文件
<div class="language" hidden>
<p class="toggle" lang='zh' >{
:lang('简体中文')}</p>
<p class="toggle" lang='en' >{
:lang('English')}</p>
</div>
$('.toggle').click(function(){
var data={
'lang':$(this).attr('lang')};
$.get("{:url('user/index/lang')}",data,function(){
location.reload();
})
})
然后在控制器中接受到传递过来请求的参数:
// 多语言切换
public function lang() {
switch ($_GET['lang']) {
// 根据传递过来的参数来判断
case 'en': // 英文
cookie('think_var','en-us'); // 通过cookie来设置语言
break;
case 'zh': // 简体中文
cookie('think_var','zh-cn');
break;
default:
break;
}
}
点击切换就可以实现中英文切换啦
效果如下图:

边栏推荐
- Depens:*** but it is not going to be installed
- Squid proxy application
- 零基础自学SQL课程 | 子查询
- Thinkphp5清除runtime下的cache缓存,temp缓存,log缓存
- 牛客网 实现简单计算器功能
- When to use RDD and dataframe/dataset
- Spark - the number of leftouterjoin results is inconsistent with that of the left table
- Essay - Reflection
- Leetcode -- linked list
- Redis实现全局唯一ID
猜你喜欢

Numpy numpy中的np.c_和np.r_详解
![[bug] @jsonformat has a problem that the date is less than one day when it is used](/img/09/516799972cd3c18795826199aabc9b.png)
[bug] @jsonformat has a problem that the date is less than one day when it is used

Huawei Router: IPSec Technology

【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试

活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
![[use picgo+ Tencent cloud object to store cos as a map bed]](/img/14/d650960cc77385504ea5e2e138bd46.jpg)
[use picgo+ Tencent cloud object to store cos as a map bed]

Epidemic situation, unemployment, 2022, we shouted to lie down!

Redis实现全局唯一ID

Solution: the word of jmeter5.5 on the win11 lower interface is very small

支持向量机(SVC,NuSVC,LinearSVC)
随机推荐
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
How to import MDF and LDF files into MySQL workbench
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
【LeetCode】541. Reverse string II
Easyexcel single sheet and multi sheet writing
Threejs glow channel 01 (unrealbroompass & layers)
4275. Dijkstra sequence
Niuke network realizes simple calculator function
学习太极创客 — ESP8226 (十三)OTA
Squid代理服务器应用
零基础自学SQL课程 | HAVING子句
RISC-V架构下 FPU Context 的动态保存和恢复
Squid proxy application
Project deployment related
Common emoticons
CF566E-Restoring Map【bitset】
零基础自学SQL课程 | 相关子查询
Jincang KFS replicator installation (oracle-kes)
Implementation process of tcpdump packet capturing
Tools