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.
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.
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.
No there isn't. The list of available cultures is system-specific - you can even register new custom cultures using CultureAndRegionInfoBuilder.
So if you want this, you'll have to create your own enum or constants for the subset of common cultures that you're interested in, e.g.:
public static class KnownCulture
{
public readonly String EnglishUS = "en-US";
public readonly String EnglishGB = "en-GB";
... etc ...
}
or
public enum KnownLCID
{
EnglishUS = 0x409,
EnglishGB = 0x809,
...
}
This is analogous to the KnownColor
enumeration: it's not possible to create an enumeration for all possible colors, but it can make sense to have an enumeration for frequently used ones.
I wouldn't expect Microsoft to provide an equivalent KnownCulture
enumeration out of the box, as it's rather sensitive (why is my culture not included?).