Get CultureInfo object from country name or RegionInfo object

Ben picture Ben · Jan 19, 2012 · Viewed 27.2k times · Source

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.

Answer

Anderson Pimentel picture Anderson Pimentel · Jan 19, 2012

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