当前位置:网站首页>php 网站的多语言设置(IP地址区分国内国外)
php 网站的多语言设置(IP地址区分国内国外)
2022-07-31 02:33:00 【逸曦穆泽】
序:本来,以为做一个语言切换的按钮就可以一直安枕无忧了,突然上头说要来一个区分国内与国外的,在国内访问的显示中文,在国外访问的显示英文。好吧!找一下资料看看。
tp5,需求是按钮切换的,选择参考3;
方法一:IP查询网
代码实现( 参考3有完整的)
// 多语言 将 think_val 自定义设置为 customLang
function lang(){
$lang = input('lang');
if(!$lang){
// 默认:think_var
if(Cookie::has("customLang")){
$lang = Cookie::get("customLang");
}else{
// $lang = "zh-cn";
$ip = request() -> ip(0,true);
$datatype = 'json';
$url = 'https://api.ip138.com/ip/?ip='.$ip.'&datatype='.$datatype;
$header = array('token:你的token');
$obj = $this-> getRequest($url,$header);
$objson = json_decode($obj,true);
$isAddress = $objson['data'][0];
if($isAddress == "中国"){
$lang = "zh-cn";
}else{
$lang = "en-us";
}
}
}
$lang = Lang::range($lang);//设定当前语言
Lang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//加载当前语言包
Cookie::set('customLang',$lang);
return $lang;
}
方法二:
maxmind GeoIP :先使用邮箱注册一个账号,并登录账号,创建一个密钥,名称就像一个备注,它会生成一个ID和密钥,记得保存好,它只显示一次;
1、登录后就可以下载一个数据包了,数据包是每周二更新的:
2、获取代码支持:
1、安装:
composer require geoip2/geoip2:~2.0
2、下载数据库,需要注册邮箱账号登录:
https://www.maxmind.com/en/accounts/722009/geoip/downloads
3、代码实现:use GeoIp2\Database\Reader;
function lang(){
$lang = input('lang');
if(!$lang){
if(Cookie::has("lenze_lang")){
$lang = Cookie::get("lenze_lang");
}else{
$ip = request() -> ip(0,true);
// maxmind 获取判断
$reader = new Reader('./public/static/GeoLite2-Country.mmdb'); // 解压的数据包
$record = $reader->country($ip);
$isAddress = $record->country->isoCode;
if($isAddress == "cn" || $isAddress == "CN"){
$lang = "zh-cn";
}else{
$lang = "en-us";
}
}
}
$lang = Lang::range($lang);//设定当前语言
Lang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//加载当前语言包
Cookie::set('lenze_lang',$lang);
return json(['lang'=>$lang]);
}
方法三:
可以使用高德地图的IP定位,不过,高德需要搜索的IP地址(仅支持国内),刚好,搜索得到的就判断为国内,搜索数据为空的为国外。
IP定位-API文档-开发指南-Web服务 API | 高德地图API
这个有开发文档了,就不用我多唠叨和做无用功了,嘎嘎!
参考:
1、最新多种方式, 判断客户端IP是国内还是国外?_解忧杂货铺Q的博客-CSDN博客_判断ip是国内还是国外
边栏推荐
猜你喜欢
基于FPGA的售货机
STP选举(步骤+案列)详解
局域网电脑硬件信息收集工具
The final exam first year course
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
基于FPGA的图像实时采集
CMOS和TTL的区别?
Installation, start and stop of redis7 under Linux
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
真正的CTO,是一个懂产品的技术人
随机推荐
String为什么不可变?
[1153]mysql中between的边界范围
mysql 视图
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
直播预告 | KDD2022博士论文奖冠亚军对话
What level of software testing does it take to get a 9K job?
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
Draw Your Cards
数学解决——环形链表问题
mysql index
Clustering index, and what is the difference between a clustering index
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
力扣刷题之爬楼梯(7/30)
STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
Crypto Life, a day in the life of a Web3 project partner
Calculate S=a+aa+…+aa…a
StringJoiner详解
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
Drools WorkBench的简介与使用