在PHP中检测客户端语言环境的最简单方法 [英] Simplest way to detect client locale in PHP

查看:137
本文介绍了在PHP中检测客户端语言环境的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用PHP在我的网站上检测访问者来自哪个国家.

请注意,我并没有试图将其用作安全措施或用于任何重要的事情,而只是更改某些单词的拼写(美国人似乎认为入学"一词有2个Ls ... ..,并且可能会在选择您的国家/地区"列表中提供默认选项.

因此,使用Geolocation数据库有点麻烦,我真的不想为此而安装新的PHP库,所以什么是最简单/最简单的查找方法访客来自哪个国家?

解决方案

不保证,但是大多数浏览器会提交一个接受语言HTTP标头,用于指定来自美国的en-us.不过,某些较旧的浏览器仅表示已启用.并非所有机器都正确设置以指示它们喜欢的语言环境.但这是一个很好的第一个猜测.

基于英语-英国的用户通常将其系统或用户区域设置设置为英语-英国,这在默认浏览器配置中应导致en-gb作为接受语言"标头. (很早以前说过en-uk,这是一个错字,很抱歉.)其他国家/地区也有en区域设置,例如en-za(南非),并且从理论上讲,也可以使用en-jp之类的组合.

但是,基于地理IP的猜测在首选语言/语言环境中不太可能是正确的. Google认为,基于IP地址地理位置的内容协商很有意义,当我在日本或韩国时,这确实让我很烦...

I would like to be able to detect what country a visitor is from on my website, using PHP.

Please note that I'm not trying to use this as a security measure or for anything important, just changing the spelling of some words (Americans seems to believe that the word "enrolment" has 2 Ls.... crazy yanks), and perhaps to give a default option in a "Select your country" list.

As such, using a Geolocation database is a tad over-the-top and I really don't want to muck about with installing new PHP libraries just for this, so what's the easiest/simplest way to find what country a visitor is from?

解决方案

Not guaranteed, but most browsers submit an Accept-Language HTTP header that specifies en-us if they're from the US. Some older browsers only said they are en, though. And not all machines are set up correctly to indicate which locale they prefer. But it's a good first guess.

English-UK based-users usually set their system or user locale to English-UK, which in default browser configurations should result in en-gb as the Accept Language header. (An earlier version of this said en-uk; that was a typo, sorry.) Other countries also have en locales, such as en-za (south africa), and, primarily theoretically, combinations like en-jp are also possible.

Geo-IP based guesses will less likely be correct on the preferred language/locale, however. Google thinks that content-negotiation based on IP address geolocation makes sense, which really annoys me when I'm in Japan or Korea...

这篇关于在PHP中检测客户端语言环境的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆