Culture Information (System.Globalization) with C#

System.Globalization name space helps us to work with different environments our application will be deployed and ran. With the globalization of world economies, globalizing application we use for business and other reasons becomes very important.

ControlInfo class is one of the most important classes within System.Globalization namespace. It allows handling string, numbers, dates comparisons and formats as well as how resources are used.

There are three different categories of culture.

  • Invariant – it is being used as generic culture or culture-insensitive. There is no specific culture that is associated with the Invariant culture but it closely matches English.
  • Neutral – English(en); Spanish(sp) and French(fr) are all examples of neutral cultures. This is purely language association and not geographical. For instance, English is spoken in many countries, so (en) will apply to USA, UK and Canada.
  • Specific – this is the most precise type of culture and represented like this: fr-FR, en-US. As we can see, here we have combination of location and language which makes it the best kind of type to use.
We detect currently running culture by using CurrentCulture property of the executing thread's CurrentThread property. There is one more property CurrentUICulture which cannot be manipulated when you run your application. You set it only once in the main method.

CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("Current culture is : " + MyUsersCulture.Name);

The best way to handle strings with culture sensitive information, such as dates, money, and time is to use conversion techniques

tbSalary.Text = (100000).ToString("C");

Setting current culture is similar to getting one

CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("The culture: "+ MyUsersCulture.Name);
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-VE");
ConsoleWriteLine("The culture: " + Thread.CurrentThread.CurrentCulture);

CultureTypes Enumeration has FlagsAttribute attribute. We can use it to display culture information.

foreach (CultureInfo MyUsersCulture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    Console.WriteLine("Culture: " + MyUsersCulture.Name);
}

Enumeration

 

NameDescription
AllCultures All available cultures
FrameworkCultures Neutral and specific cultures
InstalledWin32Cultures All cultures installed
NeutralCultures Cultures for a language
ReplacementCultures Custom cultures
SpecificCultures Cultures for a country/region
UserCustomCulture Custom cultures created by the user
WindowsOnlyCultures Cultures installed in the Windows OS

RegionInfo Class provides more granular information about culture.

CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;
RegionInfo MyDemoRegion = new RegionInfo(MyUsersCulture.LCID);
Console.WriteLine("English: " + MyDemoRegion.EnglishName);
Console.WriteLine("Display: " + MyDemoRegion.DisplayName);
Console.WriteLine("Currency: " + MyDemoRegion.CurrencySymbol);

DateTimeFormatInfo and NumberFormatInfo Classes provide a set of methods and properties to handle dates and numbers of various cultures.

CultureInfo MyUsersCulture = new CultureInfo("es-VE");
String[] Days = MyUsersCulture.DateTimeFormat.DayNames;
foreach (String Day in Days)
{
    Console.WriteLine(" Spanish : " + Day);
}

CompareInfo Class and CompareOptions Enumeration useful for Culturally Aware Comparisons. The are both instances of CompareInfo class

String FirstString = "Coté";
String SecondString = "coté";
CompareInfo DemoInfo = new CultureInfo("fr-FR").CompareInfo;
DemoInfo.Compare(FirstString, SecondString, CompareOptions.IgnoreCase);

CompareOptions Enumerations

 

NameDescription
IgnoreCase Ignore case
IgnoreKanaType Ignore the Kana type
IgnoreNonSpace Ignore nonspacing combining characters
IgnoreSymbols Ignore symbols
IgnoreWidth Ignore the character width
None String comparisons
Ordinal Comparison using the Unicode values
OrdinalIgnoreCase Comparison must ignore case
StringSort Comparison must use the string sort algorithm