问题
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 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.)
回答2:
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...
来源:https://stackoverflow.com/questions/1876103/i-have-a-list-of-country-codes-and-a-list-of-language-codes-how-do-i-map-from-c