开发者

How to recognize user language in PHP

Is there any way to parse the HTTP_USER_AGENT 开发者_如何学编程to get the current user language ?


Try:

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


You may want to try HTTP_ACCEPT_LANGUAGE in the $_SERVER superglobal.

Take a look at http://php.net/manual/en/reserved.variables.server.php for more information.

This will return a value like 'en-us' which you can then break down as needed.


In theory, you could try to do some gymnastics around the User Agent, but there is also a Accept-Language header that would seem to do the trick!


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. The most common database for this is 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜