Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have a list of country codes and a list of language codes. How do I map from country code to language code?

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.

like image 540
prismofeverything Avatar asked Dec 09 '09 19:12

prismofeverything


1 Answers

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.)

like image 67
Laurence Gonsalves Avatar answered Oct 04 '22 15:10

Laurence Gonsalves