我有一个国家代码列表和一个语言代码列表.如何从国家代码映射到语言代码? [英] I have a list of country codes and a list of language codes. How do I map from country code to language code?

查看:25
本文介绍了我有一个国家代码列表和一个语言代码列表.如何从国家代码映射到语言代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问该网站时,我可以获得他们的国家/地区代码.我想用它来设置默认语言(他们可以稍后在必要时进行修改,只是根据他们所在的国家/地区对他们可能说的语言进行一般猜测).

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.

推荐答案

搜索国家语言映射"会出现这个 按国家/地区划分的语言 表,您可以通过一些工作将其转换为语言代码.(不到 200 个国家/地区.)

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

警告:这样做肯定会惹恼瑞士或加拿大等多语言国家/地区的许多用户.对于这些国家/地区,习惯上让用户在 UI 的主页上选择他们的语言.请参阅 Google 瑞士Best Buy Canada 的一些例子.这也惹恼了外籍人士和旅行者.

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.

更好的是,为什么不使用 Accept-Language HTTP 标头来确定要使用的语言?Accept-Language 标头是确定用户语言时要查看的正确内容.它甚至会按偏好顺序为您提供一个列表,因此如果用户最流利地使用您不支持的某种语言,但相当流利地使用您支持的其他语言,您可以正确回退.Accept-Language 的一个问题是大多数用户将其保留为默认设置.不过,大多数浏览器应该默认使用平台的语言设置,所以还不错.(一些较旧的浏览器会默认使用英语,这有点糟糕,因为en"实际上意味着未设置 Accept-Language.)

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

这篇关于我有一个国家代码列表和一个语言代码列表.如何从国家代码映射到语言代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆