Get CultureInfo object from country name or RegionInfo object

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 just the country code. It doesn’t matter which exact culture I get (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 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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 CH to prevent an edge case, as pointed out by @JeppeStigNielsen (see comments below).

Method 2

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

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.

Method 3

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x