4

So I can't for example set the default to english, like so

CultureInfo MyCulture =  CultureInfo.ENGLISH;

or

CultureInfo MyCulture =  CultureInfo.getCultureInfo(CulureInfo.Eng);

I have to introduce a string dependency, using a list I found on the internet (or create my own enum) :

CultureInfo MyCulture =  CultureInfo.getCultureInfo("en-AU");

So is there already an enum or other kind of list of all available CultureInfos?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311

1 Answers1

3

You can obtain iso languages and region names, using System.Globalization classes:

System.Globalization.CultureInfo[] cinfo = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures & ~System.Globalization.CultureTypes.NeutralCultures);
System.Globalization.RegionInfo ri = null;
foreach (System.Globalization.CultureInfo cul in cinfo)
{   
    try
    {
        ri = new System.Globalization.RegionInfo(cul.Name);
        Console.WriteLine(cul.TwoLetterISOLanguageName+"-"+ri.TwoLetterISORegionName);
    }
    catch(ArgumentException)
    {
        continue;
    }
}
  • 6
    Seeing a "catch all" always makes me cringe, even in sample code (that gets copy and pasted into prod code much too often, BTW). Please consider to only catch `ArgumentException` as documented by MSDN. – Christian.K Jun 18 '14 at 10:56
  • Why do you want to try to create the `RegionInfo` instance? You already have the format `[languagecode]-[CountryCode]` in your `cul.Name` or `cul.ToString()`, so just write that out. Note: You explicitly took away the neutral cultures which do not have the country-code part. – Jeppe Stig Nielsen Jun 18 '14 at 11:51