为什么不是所有国家/地区都显示在 CultureInfo.GetCultures() 中?
我正在使用这个标准代码来填充国家/地区列表:
static void Main(string[] args)
{
List cultureList = new List();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
foreach (CultureInfo culture in cultures)
{
try
{
RegionInfo region = new RegionInfo(culture.LCID);
if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region.EnglishName);
Console.WriteLine(region.EnglishName);
}
}
catch (ArgumentException ex)
{
// just ignore this
continue;
}
}
}
我发现有些国家/地区被遗漏了。只是想知道造成这种情况的原因是什么?
I am using this standard code for populating list of countries:
static void Main(string[] args)
{
List cultureList = new List();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
foreach (CultureInfo culture in cultures)
{
try
{
RegionInfo region = new RegionInfo(culture.LCID);
if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region.EnglishName);
Console.WriteLine(region.EnglishName);
}
}
catch (ArgumentException ex)
{
// just ignore this
continue;
}
}
}
I saw that some countries are missed. Just wondered what's the reason of such situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是:从设计上来说
,
CultureInfo.GetCultures
并不是被设计为世界上所有文化的完整且明确的列表。它仅旨在为您提供可以在计算机上找到的文化。CultureInfo 文档 说:
注释
MSDN 上可能有用的链接:
顺便说一句,您可以使用简单的 LINQ“命令”来缩短代码:
The answer is: By design
CultureInfo.GetCultures
is not designed to be a complete and definitive list of all the cultures in the world. It's only designed to get you the cultures that can be found on the computer.CultureInfo documentation says:
Notes
Links on the MSDN that may be usefull:
And by the way, you can shorten your code with a simple LINQ 'command':
您没有获得所有文化:
You are not getting all cultures:
我会使用 CultureTypes.SpecificCultures 但它不能回答你的问题。
为什么世界上只有一小部分国家?嗯,他们有很多。必须有人来维护它们,这确实要花钱。我认为这就是微软决定只支持最“流行”的原因。
顺便提一句。您可以创建自己的 CultureInfo。另外,我还没有尝试过,但您可以通过在构造函数中传递 ISO 代码来创建 RegionInfo 实例。不过,我不确定如果没有匹配的 CultureInfo 会发生什么。
I would use CultureTypes.SpecificCultures but it does not answer your question.
Why there is only subset of world's countries? Well, there are so many of them. Somebody would have to maintain them and it does cost money. I think that's why Microsoft decided to support only the most "popular" ones.
BTW. You may create your own CultureInfo. Also, I haven't tried, but you can create RegionInfo instance by passing its ISO code in constructor. I am not sure what will happen if there is no matching CultureInfo, though.