views:

415

answers:

4

When the user visits the site, I can get their country code. I want to use this to set the default language (which they can later modify if necessary, just a general guess as to what language they might speak based on what country they are in).

Is there a definitive mapping from country codes to language codes that exists somewhere? I could not find it. I know that not everyone in a particular country speaks the same language, but I just need a general mapping, the user can select their language manually later.

+1  A: 

I found something close on Wikipedia: http://en.wikipedia.org/wiki/List_of_official_languages_by_state

This will obviously need some cross-indexing against a list of country names and codes:

http://www.nationsonline.org/oneworld/country_code_list.htm

...and language codes: http://www.ethnologue.com/codes/LanguageCodes.tab

Oops... that last link mostly satisfies your request, I think. Take a look!

Carl Smotricz
+5  A: 

A search for "country language mapping" turns up this Languages by Countries table, which you could probably convert to language codes with a bit of work. (There are less than 200 countries.)

A warning though: doing this is pretty much guaranteed to annoy many users in multi-lingual countries like Switzerland or Canada. For such countries it's customary to let the user choose their language on the main page of the UI. See Google Switzerland or Best Buy Canada for some examples. This also annoys expatriates and travelers.

Better yet, why don't you use the Accept-Language HTTP header to determine which language to use? The Accept-Language header is the Right Thing to look at when determining the user's language. It even gives you a list in order of preference, so if the user is most fluent in some language you don't support, but is reasonably fluent in some other language that you do support you can fall back correctly. The one problem with Accept-Language is that most users leave it at the default setting. Most browser should default to platform's language setting, though, so it isn't too bad. (Some older browsers would default to English, which was kind of a disaster because "en" effectively meant the Accept-Language was unset.)

Laurence Gonsalves
**Accept-Language HTTP header** is the right way not the other one. It would be nice if you could edit your answer and explain this. Choosing language based on the country is a very wrong way.
Sorin Sbarnea
@Sorin I thought it already said that, but ok, I've edited to elaborate on why Accept-Language is god and country mapping isn't.
Laurence Gonsalves
@Laurence I was just asking for some bold to make the main message easier to read for those who are lazy reading your full post ;)
Sorin Sbarnea
+4  A: 

While there may be good reasons to try and map countries (or indeed specific locations/areas within a country) to languages, it seems that for your purpose, using the

HTTP Request header Accept-Language,

if only as a one of the elements in your language selection heuristic (along with mabye teh country code) may be an easy and safe choice...

This approach occasionally errs because the default language(s) associated with a given web browser installation (when the underlying user has not taken the time to select a installation package / setup for his/her preferred natural language). On the other hand it also provides a better language hint for many expatriates (who have install / setup a browser in accordance to preferred language) even though the geolocation / language association would be totally wrong.

In general this approach should outperform methods solely based on geolocation (and on a lookup table), in the many countries such as Switzerland, Luxembourg... as well as many metropolitan areas say New York, Geneva or Miami...

mjv