views:

61

answers:

3

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

A: 

You pretty much answered your own question - you can either use that header OR just geoip (it's dead simple to use)

Download from: http://geolite.maxmind.com/download/geoip/api/php/

Sample code:

include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, $user_ip);
echo "The users country code is:" . $record->country_code;
geoip_close($gi);
Yawn
Geoip is only a way to guess when no user preference is available. This matters because some locations have several possible reasonable languages available. (Should you prefer English or Spanish for a Miami address? There's just no way to know without asking. User preferences are simply the best way to do that asking.)
Donal Fellows
A: 

I'd check both the accept language header and the originating country of the IP address of the visitor. If the accept language header is anything but english (probably the most used default setting), use that to set the default language. If the accept language is english, use the originating country to set the default language. I think this will give you a more educated guess at the visitors native language.

Victor Welling
+3  A: 

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.
chryss
Brilliant. Thank you for a very comprehensive answer.
Eric