简体   繁体   English

如何在PHP中识别用户语言

[英]How to recognize user language in PHP

有没有办法解析HTTP_USER_AGENT以获取当前的用户语言?

Try: 尝试:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
echo $lang;

You may want to try HTTP_ACCEPT_LANGUAGE in the $_SERVER superglobal. 您可能想在$ _SERVER超全局中尝试HTTP_ACCEPT_LANGUAGE。

Take a look at http://php.net/manual/en/reserved.variables.server.php for more information. 有关更多信息,请查看http://php.net/manual/en/reserved.variables.server.php

This will return a value like 'en-us' which you can then break down as needed. 这将返回一个类似'en-us'的值,然后您可以根据需要进行分解。

从理论上讲,你可以尝试在用户代理周围做一些体操,但是还有一个似乎可以解决问题的Accept-Language标题!

Another common way (as to create an alternative) to do this is to check the user's IP against a database of IP's with associated regions. 另一种常见的方法(如创建备选方案)是针对具有关联区域的IP数据库检查用户的IP。 The most common database for this is GeoIP ( http://www.maxmind.com ). 最常见的数据库是GeoIP( http://www.maxmind.com )。 It's worth taking a look at if you're interested. 如果你有兴趣,值得一看。 You can then proceed in changing the language to that of the region. 然后,您可以继续将语言更改为该区域的语言。

Regards, 问候,
Dennis M. 丹尼斯·M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM