Constants for CultureInfo Name

Annie B picture Annie B · Sep 27, 2012 · Viewed 12.6k times · Source

Is there a set of constants or an enumeration in C# system/globalization namespace which contains valid culture names?

I am looking for something so that I don't have to type in "en-GB", etc.

Answer

McGarnagle picture McGarnagle · Sep 27, 2012

Yes, there is, GetCultures:

System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures)

That returns an array of CultureInfo objects, so if you want the string names you could use something like:

IEnumerable<CultureInfo> cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
string[] names = cultures.Select(c => c.Name).ToArray();

Note the "Culture Types" enum (from the MSDN link). I suppose the most useful ones would be NeutralCultures and SpecificCultures.

  • NeutralCultures Cultures that are associated with a language but are not specific to a country/region. The names of .NET Framework cultures consist of the lowercase two-letter code derived from ISO 639-1. For example: "en" (English) is a neutral culture. Custom cultures can have any user-specified name, not just a two-letter code. The invariant culture is included in the array of cultures returned by the CultureInfo.GetCultures method that specifies this value.
  • SpecificCultures Cultures that are specific to a country/region. The names of these cultures follow RFC 4646 (Windows Vista and later). The format is "-", where is a lowercase two-letter code derived from ISO 639-1 and is an uppercase two-letter code derived from ISO 3166. For example, "en-US" for English (United States) is a specific culture. Custom cultures can have any user-specified name, not just a standard-compliant name.
  • InstalledWin32Cultures All cultures that are installed in the Windows operating system. Note that not all cultures supported by the .NET Framework are installed in the operating system.
  • AllCultures All cultures that ship with the .NET Framework, including neutral and specific cultures, cultures installed in the Windows operating system, and custom cultures created by the user.
  • UserCustomCulture Custom cultures created by the user.
  • ReplacementCultures Custom cultures created by the user that replace cultures shipped with the .NET Framework.