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

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

问题描述

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

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

尽管如此,但警告:这样做肯定可以使多语言国家(例如瑞士或加拿大)的许多用户烦恼.对于此类国家/地区来说,习惯上是让用户在用户界面的主页上选择他们的语言.请参见 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标头是确定用户语言时要注意的事情.它甚至按优先顺序提供了一个列表,因此,如果用户最熟练地使用您不支持的某种语言,但可以合理地使用您所支持的某种其他语言,则可以正确使用.接受语言的一个问题是大多数用户将其保留为默认设置.不过,大多数浏览器应默认使用平台的语言设置,因此还不错. (某些较旧的浏览器将默认使用英语,这是一种灾难,因为"en"实际上意味着未设置接受语言.)

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天全站免登陆