22 December 2011

Obtaining Culture Information through FormatProviders

Often we come across the situations where we need to read the culture specific information like date/time format, decimal seperator, currency symbol, number styles etc. Mostly we need this information for formatting the output according to user culture. In this post, I will show you how you can obtain such culture specific information.
That said, without spending much time in theory, let's see where we find this information. As you might know, System.Globalization.CultureInfo class is the one you must target for this purpose. This class, CultureInfo, provides a public function called GetFormat (a method declared in IFormatProvider interface) whose signture looks like below,
 public virtual object GetFormat(Type formatType);
As of now, formatType paramter can be either NumberFormatInfo or DataTimeFormatInfo both of which are defined in System.Globalization namespace. The NumberFormatInfo class provides you information about number system such as Positive Sign, Negetive Sign, Decimal seperator, Currency Symbo, Native Digits etc. On the other hand DateTimeFormatInfo provides information about Date and Time formats like, DateSeperator, FirstDayOfWeek, LongDate format, ShortDate format, Time Seperator etc.
 
For example, here is how I get the currency symbol for the culture fr-FR.
 Type formatType = typeof(NumberFormatInfo);  
 CultureInfo frCulture = CultureInfo.GetCultureInfo("fr-FR");  
 NumberFormatInfo info = frCulture.GetFormat(formatType) as NumberFormatInfo;  
 String currencySymbol = info.CurrencySymbol;
I think the above example gives you the whole idea on how to get culture specific information. Below is the code that will give you all DateTime format info that is possible to get from current culture. You can use this code for NumberFormatInfo as well (just replace DateTimeFormatInfo with NumberFormatInfo).

 public String GetDateTimeFormatInfo(String cultureName)  
 {  
   CultureInfo cultInfo = CultureInfo.GetCultureInfo(cultureName);  
   var formatInfo = (DateTimeFormatInfo)cultInfo.GetFormat(typeof(DateTimeFormatInfo));  
   PropertyInfo[] properties = formatInfo.GetType().GetProperties();  
   String info = String.Empty;  
   foreach (var prop in properties)  
   {  
      if (prop.GetValue(formatInfo, null) is Array)  
      {  
         Array a = prop.GetValue(formatInfo, null) as Array;  
         String values = String.Empty;  
         foreach (var item in a)  
         {  
            values += item + ",";  
         }  
         info += prop.Name+":"+values.Substring(0,values.Length-1);  
         info += Environment.NewLine;  
      }  
      else  
      {  
         info += prop.Name + " : " + prop.GetValue(formatInfo, null);  
         info += Environment.NewLine;  
      }  
   }  
   return info;  
 }  
Note: You  can also obtain all this information from registry. If you are keen, you can have examine the below registry key.

 HKEY_CURRENT_USER\Control Panel\International  

1 comment: