当前位置:网站首页>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.02. 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
边栏推荐
猜你喜欢

Static routing + PAT + static NAT (explanation + experiment)

The application of AI in the whole process of medical imaging equipment

There is a problem with the multiplayer-hlap package and the solution cannot be upgraded

What level of software testing does it take to get a 9K job?

Basic learning about Redis related content

The effective square of the test (one question of the day 7/29)

你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?

Intranet Infiltration - Privilege Escalation

MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi

静态路由解析(最长掩码匹配原则+主备路由)
随机推荐
AtCoder Beginner Contest 261 Partial Solution
Coldfusion file read holes (CVE - 2010-2861)
Calculate S=a+aa+…+aa…a
Installation, start and stop of redis7 under Linux
Drools basic introduction, introductory case, basic syntax
LeetCode 每日一题 2022/7/25-2022/7/31
What level of software testing does it take to get a 9K job?
Face detection based on opencv
Android's webview cache related knowledge collection
Why is String immutable?
怎样做好一个创业公司CTO?
Difference between CMOS and TTL?
Drools基本介绍,入门案例,基本语法
LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路
Teach you how to configure Jenkins automated email notifications
The Sad History of Image Processing Technology
The final exam first year course
Manchester City confuses fans with smart scarf that detects emotions
Fiddler captures packets to simulate weak network environment testing
7. List of private messages