18

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.

Ben
  • 511
  • 2
  • 6
  • 18

3 Answers3

26

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

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
  • useful **`iso2 = "US";`** Code:`var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.Name.EndsWith(iso2)).ToList(); foreach (System.Globalization.CultureInfo culture in cultureList) { Response.Write("
    Culture for " + iso2 + ": " + culture.DisplayName + " *** " + culture.NativeName + " " + culture.TwoLetterISOLanguageName + " - " + culture.ThreeLetterISOLanguageName + " - " + culture.DateTimeFormat.FullDateTimePattern); }`
    – Kiquenet Dec 22 '15 at 12:52
  • 1
    This question kind-of deserves an ugly answer (and I just posted another one), but still: When I try your code with __Haiti__, country code `"HT"`, one of the cultures I get back is `"zh-CHT"`, __Chinese (Traditional) Legacy__, known natively as __中文(繁體) 舊版__. – Jeppe Stig Nielsen Feb 01 '17 at 18:27
  • @JeppeStigNielsen Changing the code above to `EndsWith("-CH")` would to the trick or you can think of another edge case? – Anderson Pimentel Feb 01 '17 at 19:50
  • 1
    I do not think there will be any examples if you do that. Even if some neutral cultures contain a hyphen, for example `"sr-Cyrl"`, __Serbian (Cyrillic)__ (no country specified), or `"mn-Mong"`, __Mongolian (Traditional Mongolian)__ (no country specified), where the second part specifies the writing system because the language can be written with different scripts, the second part will have _more_ than two letters in that case. – Jeppe Stig Nielsen Feb 01 '17 at 20:32
  • This is incredibly fragile. Do not rely on string existence checks, you'll break somewhere. – Tanveer Badar May 27 '21 at 09:03
6

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Fabio
  • 11,892
  • 1
  • 25
  • 41
  • 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. But it must be dynamic. – Ben Jan 19 '12 at 13:14
  • Can I get `CultureInfo` using **HKCU\Control Panel\International\Geo** - ***Nation*** value ? – Kiquenet Dec 22 '15 at 13:10
5

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)

Edit: A later version gives the above, plus these two:

en-CH: English (Switzerland)
pt-CH: Portuguese (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).

Edit: Unsurprisingly, the second method has changed in some versions of .NET. Something like:

var data = typeof(RegionInfo).GetField("_cultureData", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(region);

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

or (if you use nullable reference types) with a lot of !:

var data = typeof(RegionInfo).GetField("_cultureData", BindingFlags.NonPublic | BindingFlags.Instance)
    !.GetValue(region);

var name = (string)(data!.GetType().GetProperty("CultureName", BindingFlags.NonPublic | BindingFlags.Instance)
    !.GetValue(data)!);
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181