Main Content

Finding the language preference of a web site visitor

Archive - Originally posted on "The Horse's Mouth" - 2006-06-18 07:17:31 - Graham Ellis

I can (and do) identify the country of origin of over 98% of the visitors to our web site using the maxmind database. But does that tell me what their native language is? No - it only gives me a clue.

A further clue is available from the accept-language header that's returned by browsers, and is available in PHP in the variable $_SERVER[HTTP_ACCEPT_LANGUAGE]. With this variable, your web site can check if the user has expressed a language preference (and perhaps given a second and third choice too), and you can use the information to tailor your response as appropriate.

I've put up sample source code and a working demonstration on our web site. I've also added a page reporting language statistics for our own site ... only a very short data sample on there today, but as from tomoorow it will update daily with a 24 hour report.

Some surpises ... I would expect to find most major European countries choosing their own langauges, with Swiss visitors choosing between German, Italian and English. The first tests I ran, though, gave me a series of pages called up from the USA with a first choice language of Korean. Truly, the world is a global market.