views:

42

answers:

4

Is there any way to parse the HTTP_USER_AGENT to get the current user language ?

+4  A: 

Try:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
echo $lang;
Sarfraz
+1 That's the easiest solution if all you need is to detect the most supported language.
Maerlyn
HTTP_ACCEPT_LANGUAGE: 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4'g8 this is good solution for this things that i currently whant to do becouse i only have to recognize one languge so i check the existing of that language in this string and it works.But i have question:what the whole string stands for what are the q poperties ?
Galileox86
+1  A: 

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!

MikeD
+2  A: 

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.

sparkey0
A: 

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.

RageD
Checking the `HTTP_ACCEPT_LANGUAGE` is far more reliable, especially with people travelling to foreign countries.
Maerlyn
*Don't* do this! There is no guarantee that someone visiting from any specific IP is actually located where the IP is registered. Beyond visiting other countries, think of proxy servers and countries with restrictive national firewalls.
Charles
Just as an alternative, as the first line of my post says. I do agree that HTTP_ACCEPT_LANGUAGE is more reliable.
RageD