C# 从国家名称或 RegionInfo 对象中获取 CultureInfo 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8926400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:13:38  来源:igfitidea点击:

Get CultureInfo object from country name or RegionInfo object

c#asp.netcultureregion

提问by Ben

Given a specific country code, e.g. "CH", how can I get a CultureInfo object? The specific country code is dynamic (changes at runtime). I only have the country code, and i want to know if it is possible to create a CultureInfo object from justthe country code. It doesn't matter which exact culture I get (fr-CH/de-CH).

给定特定的国家/地区代码,例如“CH”,如何获取 CultureInfo 对象?特定的国家/地区代码是动态的(在运行时更改)。我只有国家/地区代码,我想知道是否可以仅从国家/地区代码创建 CultureInfo 对象。我得到哪种确切的文化并不重要(fr-CH/de-CH)。

I'm trying do something like this:

我正在尝试做这样的事情:

CultureInfo c = CultureInfo.CreateSpecificCulture("CH");

Would it be possible to create a culture from a RegionInfo object? Then it would look like this:

是否可以从 RegionInfo 对象创建文化?然后它看起来像这样:

RegionInfo r= new RegionInfo("CH");
CultureInfo c = CultureInfo.CreateSpecificCulture(r);

Obviously the preceding examples don't compile, they just give an idea of what I'm trying to achieve.

显然前面的例子不能编译,它们只是给出了我想要实现的想法。

采纳答案by Anderson Pimentel

If you only have the country code, you could use something like this to get all culture infos associated with that country:

如果您只有国家/地区代码,则可以使用类似的方法来获取与该国家/地区相关的所有文化信息:

var cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures)
                              .Where(c => c.Name.EndsWith("-CH"));

EDIT: adding -before CHto prevent an edge case, as pointed out by @JeppeStigNielsen (see comments below).

编辑:如@JeppeStigNielsen 指出的那样,-在之前添加CH以防止边缘情况(见下面的评论)。

回答by Fabio Luz

Are you trying to create a CultureInfo object ? like this:

您是否正在尝试创建 CultureInfo 对象?像这样:

CultureInfo c = new CultureInfo("de-CH"); //culture for  German (Switzerland)
CultureInfo c = new CultureInfo("fr-CH"); //culure for French (Switzerland)
CultureInfo c = new CultureInfo("it-CH"); //culture for Italian (Switzerland)

Maybe this link can be useful http://www.csharp-examples.net/culture-names/it show all Cultures.

也许此链接可能有用http://www.csharp-examples.net/culture-names/它显示所有文化。

回答by Jeppe Stig Nielsen

Of course this is an ugly thing to do because a country (including your example "CH"Switzerland) can have many languages.

当然,这是一件丑陋的事情,因为一个国家(包括您的示例"CH"瑞士)可以有多种语言。

Still, I will offer you two ugly methods. First one:

不过,我会为您提供两种丑陋的方法。第一:

static IEnumerable<CultureInfo> FindCandidateCultures(RegionInfo region)
{
  return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
    .Where(x => (new RegionInfo(x.Name)).GeoId == region.GeoId);
}

When used on your particular example, new RegionInfo("CH"), it gives, on my version of the .NET Framework and my Windows version:

在您的特定示例中使用时new RegionInfo("CH"),它会在我的 .NET Framework 版本和我的 Windows 版本上提供:

de-CH: German (Switzerland)
fr-CH: French (Switzerland)
gsw-CH: Alsatian (Switzerland)
it-CH: Italian (Switzerland)
rm-CH: Romansh (Switzerland)
wae-CH: Walser (Switzerland)

The second method I will offer you:

我将提供给您的第二种方法:

// ugly reflection, this can break any day!
static CultureInfo ConvertToCulture(RegionInfo region)
{
  var data = typeof(RegionInfo).GetField("m_cultureData", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(region);

  var name = (string)(data.GetType().GetProperty("SNAME", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(data));

  return CultureInfo.GetCultureInfo(name);
}

As is evident, it uses internal representations of mscorlib.dll, and we never know if it will break with future versions. But it does give you a way to pick a particular culture. On my machine, from new RegionInfo("CH"), you get it-CH: Italian (Switzerland).

很明显,它使用 的内部表示mscorlib.dll,我们永远不知道它是否会在未来的版本中中断。但它确实为您提供了一种选择特定文化的方法。在我的机器上,从new RegionInfo("CH"),你得到it-CH: Italian (Switzerland)