简体   繁体   中英

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.

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.

从理论上讲,你可以尝试在用户代理周围做一些体操,但是还有一个似乎可以解决问题的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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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