Galileox86
Galileox86

Reputation: 550

How to recognize user language in PHP

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

Upvotes: 3

Views: 767

Answers (4)

RageD
RageD

Reputation: 6823

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.

Upvotes: 0

sparkey0
sparkey0

Reputation: 1690

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.

Upvotes: 2

MikeD
MikeD

Reputation: 3368

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!

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382696

Try:

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

Upvotes: 5

Related Questions