使用 PHP HTTP_ACCEPT_LANGUAGE 服务器变量
我创建了一个 PHP 脚本,用于检查 HTTP_ACCEPT_LANGUAGE
并使用加载网站从第一个两个字符开始选择适当的语言:
$http_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
switch ($http_lang) {
case 'en':
$SESSION->conf['language'] = 'english';
break;
case 'es':
$SESSION->conf['language'] = 'spanish';
break;
default:
$SESSION->conf['language'] = $PREFS->conf['languages'][$SESSION->conf['language_id']];
}
如果我在 Firefox 中将语言更改为西班牙语,则网站会以西班牙语加载。不过,我收到几份报告称,哥伦比亚人看到的网站是英文的。
细节: “es-co” LCID = 9226 西班牙语(哥伦比亚)
有人知道为什么会发生这种情况吗?我认为这是检查用户支持的语言的最佳方法。
I created a PHP script that checks the HTTP_ACCEPT_LANGUAGE
and loads the website using the appropriate language from the 1st two characters:
$http_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
switch ($http_lang) {
case 'en':
$SESSION->conf['language'] = 'english';
break;
case 'es':
$SESSION->conf['language'] = 'spanish';
break;
default:
$SESSION->conf['language'] = $PREFS->conf['languages'][$SESSION->conf['language_id']];
}
If I change the language to Spanish in Firefox the website loads in Spanish fine. However I have had several reports that people in Colombia see the website in english.
Details:
"es-co" LCID = 9226 Spanish(Colombia)
Anyone have any ideas as to why this is happening? I thought this was the best way to check what language users support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
更现代的方法是使用
http_negotiate_language()
:如果您不这样做安装了 http 扩展 (也不是国际的),评论中还有另一种解决方法(用户注释 #86787(2008 年 11 月;匿名者)):
A more contemporary method would be to use
http_negotiate_language()
:If you don't have the http extension installed (and not the intl one as well), there is yet another workaround in the comments (user-note #86787 (Nov 2008; by Anonymous)):
我使用了 @GabrielAnderson 的正则表达式,并设计了这个函数,该函数的行为符合 RFC 2616(当没有为语言赋予质量值时,它默认为 1)。
当多种语言具有相同的质量值时,最具体的语言会优先于不太具体的语言。 (此行为不是 RFC 的一部分,RFC 没有针对此特定情况提供任何建议)
示例:
输出:
正如您所注意到的,尽管“en”在给定字符串中位于第一个位置,但“en-US”出现在第一个位置。
因此,您可以使用此函数并将第一行代码替换为:
I used the regex from @GabrielAnderson and devised this function which behaves according to RFC 2616 (when no quality value is given to a language, it defaults to 1).
When several languages share the same quality value, the most specific are given priority over the less specific ones. (this behaviour is not part of the RFC which provides no recommendation for this specific case)
Example:
Outputs:
As you can notice, 'en-US' appears in first position despite the fact that 'en' was first in the given string.
So you could use this function and just replace your first line of code by:
您知道来自哥伦比亚的所有访问您网站的访问者是否都会发生这种情况吗?用户通常可以自由地更改浏览器的语言设置,或者让计算机的负责人为他们进行更改。正如 zerkms 建议的那样,尝试记录 IP 地址及其标头。
如果您安装了intl扩展,您可以使用
Locale::lookup
和Locale::acceptFromHttp
获取从用户浏览器设置中选择最合适的语言以及可用翻译的列表。Do you know if this is happening for all visitors to your site from Colombia? Users are usually free to alter the language settings of their browsers — or to have them altered for them by whoever is in charge of the computer. As zerkms recommends, try logging IP addresses and their headers.
If you have the intl extension installed you can use
Locale::lookup
andLocale::acceptFromHttp
to get a best-fit choice of language from the users browser settings and a list of what translations you have available.最后我选择了这个解决方案:
我要感谢 AJ 提供的链接。也感谢所有回复的人。
In the end I went with this solution:
I would like to thank AJ for the links. Also thanks to all that replied.
我将使用完整的区域设置代码来引用语言,因为
zh-TW
和zh-CN
是两种不同的语言。I will use full locale code to refer language, because like
zh-TW
andzh-CN
is 2 different language.如果你想在数组中存储语言,我这样做:
将一个数组输出到其他语言,其值
如下输出一个数组:
if you want to store languages in array, i do this:
this output an array to languages e other with values
this output an array like this:
我信任那些从事 PHP 工作并具有超前思维的熟练程序员。
这是我的 Google 翻译下拉菜单标签版本。
I put my trust in the skilled programmers who work for PHP and think ahead.
Here is my version of a label for the Google translator drop down.