Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I18n of a web site: how do I detect language?

I'm working on a web site that has some minor internationalization needs (English, French and German), and I want to figure out where the user is coming from so I know which to display. I think the right way to do this is to check the "HTTP_ACCEPT_LANGUAGE" header to find out the #1 language the user is requesting (and it's safe to assume that all users in Germany have browsers that default to asking for DE, etc.).

Is that correct? Or do I have do something else (hopefully nothing too ridiculous, like trying to figure out their country from their IP)...?

thanks-- Eric

like image 669
Eric Avatar asked Feb 26 '23 15:02

Eric


1 Answers

If you go for the currently most popular choice -- geoIP -- without further consideration, you're not doing your users a great favour.

Languages and countries do not overlap. What are you going to do with users from Swiss IP addresses? Belgium? Canada? If you go for the majority language in each case, you'll annoy repeat visitors who happen to be part of large linguistic minorities: they'll be greeted in the "wrong" language each and every time, even though the site is available in their preferred language. The same is true for expats -- and from experience, it is extremely disconcerting to be greeted in, say, Swedish just because I happen to travel to Sweden. (And even big sites get it wrong. The other day I arrived at Vancouver airport from London and Google switched me to fr_CA.) Don't forget that a large number of people are multilingual or non-native speakers of majority language of their country of residence.

Here are the guidlines I follow:

  1. First check what the user is telling you about their likely choice. The easiest ways are:
    • Parse the language/culture information out of the HTTP user agent string (examples for Firefox). This tells you something about the OS version they're running, and is a very good indication of preference. They may of course be spoofing user agents, but if they are doing that they're less likely to be surprised they're getting wrong pre-sets.
    • The Accept-Language HTTP header (lower priority, as you're likely to see more "en" here than actual users with English as the OS preference).
  2. Second, save the user's preference. If they log in, ask them and save it as an option. If not, set a cookie (with a reasonable expiry setting). This way, they will be pleasantly surprised when they come back.
  3. Use GeoIP only as the last resort.
like image 191
chryss Avatar answered Mar 08 '23 08:03

chryss