当前位置:网站首页>Multilingual settings of php website (IP address distinguishes domestic and foreign)
Multilingual settings of php website (IP address distinguishes domestic and foreign)
2022-07-31 02:40:00 【Yixi Muze】
Preface: Originally, I thought that I would have peace of mind by making a language switch button, but suddenly the above said that I would like to have one that distinguishes between domestic and foreign, and that the access in China will be displayed in Chinese, and the access in foreign countries will be displayed in English.All right!Look for information.
tp5, the requirement is switch by button, choose reference 3;
Method 1: IP query network
Code implementation (refer to 3 for complete)
// Multilingual Set think_val custom to customLangfunction lang(){$lang = input('lang');if(!$lang){// Default: think_varif(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: your token');$obj = $this->getRequest($url,$header);$objson = json_decode($obj,true);$isAddress = $objson['data'][0];if($isAddress == "China"){$lang = "zh-cn";}else{$lang = "en-us";}}}$lang = Lang::range($lang);//Set the current languageLang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//Load the current language packCookie::set('customLang',$lang);return $lang;}
Method 2:
maxmind GeoIP : Register an account with your email address and log in to your account, create a key, the name is like a note, it will generate an ID and key, remember to save it, it will only be displayed once;
1. After logging in, you can download a data package. The data package is updated every Tuesday:
2. Get code support:
1. Installation:
composer require geoip2/geoip2:~2.0
2. To download the database, you need to register an email account to log in:
https://www.maxmind.com/en/accounts/722009/geoip/downloads
3. Code implementation: 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 gets the judgment$reader = new Reader('./public/static/GeoLite2-Country.mmdb'); // decompressed packet$record = $reader->country($ip);$isAddress = $record->country->isoCode;if($isAddress == "cn" || $isAddress == "CN"){$lang = "zh-cn";}else{$lang = "en-us";}}}$lang = Lang::range($lang);//Set the current languageLang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//Load the current language packCookie::set('lenze_lang',$lang);return json(['lang'=>$lang]);}
Method three:
You can use the IP location of AutoNavi map, however, the IP address that AutoNavi needs to search (only supports domestic), just, the search result is judged as domestic, and the search data is empty is foreign.
IP Location-API Documentation-Development Guide-Web Service API | AutoNavi Map API
This one has development documentation, so I don't need to nag and do useless work, quack!
Reference:
2, tp5 multi-language switching 版权声明
本文为[Yixi Muze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207310233430265.html
边栏推荐
- The application of AI in the whole process of medical imaging equipment
- 直播预告 | KDD2022博士论文奖冠亚军对话
- Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
- [1154] How to convert string to datetime
- 19.支持向量机-优化目标和大间距直观理解
- How to design the changing system requirements
- Face detection based on opencv
- 知识蒸馏7:知识蒸馏代码详解
- The final exam first year course
- YOLOV5学习笔记(二)——环境安装+运行+训练
猜你喜欢
7、私信列表
coldfusion8 background scheduled tasks take shell
11、Redis实现关注、取消关注以及关注和粉丝列表
StringJoiner in detail
Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
FPGA-based vending machine
Introduction to flask series 】 【 flask - using SQLAlchemy
Problems that need to be solved by the tcp framework
The principle of complete replication of virtual machines (cloud computing)
图解lower_bound&upper_bound
随机推荐
The modification is not properly placed in the sandbox, causing Apple compatibility issues
YOLOV5学习笔记(二)——环境安装+运行+训练
怎样做好一个创业公司CTO?
Maximum area of solar panel od js
StringJoiner in detail
Modbus on AT32 MCU
10、Redis实现点赞(Set)和获取总点赞数
修改未正确放入沙盒造成苹果兼容性问题
Real-time image acquisition based on FPGA
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
[1154] How to convert string to datetime
multiplayer-hlap 包有问题,无法升级的解决方案
String为什么不可变?
静态路由+PAT+静态NAT(讲解+实验)
YOLOV5 study notes (3) - detailed explanation of network module
execsnoop 工具
tcp框架需要解决的问题
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
Validate XML documents
12 磁盘相关命令