Blog from Saravanan Arumugam

Let us talk about Technologies

Working with Cultures (Globalization)


Making a generic application that reach different people of different culture has always been a dream.
 
People in different regions not only use different language. The representation of Units, representation of Currency, representation of numbers, representation of Date time are all different. These representations are called cultures.
 
.Net presents a namespace that is dedicated to solve this need. System.Globalization.
 
Standard Representation of Culture Types
US english culture is represented by a string "en-US".
Here ‘en’ says the language, ‘US’ says the culture. ("language-Culture")
For example,
"en-GB" says English language with Great Britain culture
"ar-SA" says Arabic language with Saudi Arabian culture
"es-ES" says Spanish language with Spain culture.
There are something called Nutral Cultures where it takes only the language. ("en", "ar", "es")
Note: System.Globalization.CultureInfo class’s DisplayName property would help us understand these descriptions.
 
System.Globalization.CurrentInfo class
CurrentInfo class can be used to hold a culture specific information.  This call can be constructed with the Standard Culture reprentation ("language-Culture").
 
Knowing/Changing the current Culture
System.Threading.Thread.CurrentThread provides a static property CurrentCulture. This can be read or set by the application. This property takes System.Globalization.CultureInfo class (An example is presented below).
 
Formatting DateTime, Numbers and Currency
Though when we are talking about the different cultures, the application is going to be built on a single culture. But our interrest is on the Output/UI presentation.
 
While doing constructing an output, use the specific formatting to represent a specific culture information.
For example consider the following snippet.

info =

new CultureInfo("ru-RU");

sb.AppendLine((info).DisplayName);
sb.AppendLine(

"Russian Money: " + (1234567.89).ToString("C", info));

sb.AppendLine(

"Russian Date Time: " + DateTime.Now.ToString(info));

sb.AppendLine(

"Russian Numbers: " + (1234567.89).ToString(info));

In the line that prints "Russian Money: ", a floating point number is converted as currency string with Russian Culture info.

String.ToString(string format, IFormatProvider provider) overload is used here.

Note: CultureInfo has implementation of IFormatProvider.

 

The lines that print "Russian Date Time: ", "Russian Numbers: " are simply taking the ToString(IFormatProvider) overload to represent the date and numbers.

 

Customizing culture information

It is also possible to alter some of the culture specific informations. For example in the "es-GB" culture Date is represented as "dd/MM/yyyy". If we want to change the date separator, we can do so with the help of CultureInfo.DateTimeFormat.DateSeparator property.

There are a variety of methods and properties are provided within CultureInfo class to help customizing the culture information.

 

The following example illustrates various manupulations of CultureInfo class.

 

Note: I used a windows application’s MessageBox.Show() to represent these culture specific informations. Console application may not display some of the special characters in some cultures.

            StringBuilder sb = new StringBuilder();

            CultureInfo info = new CultureInfo("en-US");

            info.NumberFormat.CurrencySymbol = "Rs.";

            info.NumberFormat.CurrencyGroupSeparator = ":";

            info.DateTimeFormat.DateSeparator = "#";

            Thread.CurrentThread.CurrentCulture = info;

 

            sb.AppendLine(info.DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " + (1234567.89).ToString("C"));

            sb.AppendLine(info.DisplayName + " Date Time: " + DateTime.Now);

            sb.AppendLine(info.DisplayName + " Numbers: " + 1234567.89);

 

            sb.AppendLine();

            info = new CultureInfo("es-ES");

            sb.AppendLine(info.DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            sb.AppendLine();

            info = new CultureInfo("it-IT");

            sb.AppendLine((info).DisplayName);

 

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            sb.AppendLine();

            info = new CultureInfo("ru-RU");

            sb.AppendLine((info).DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            sb.AppendLine();

            info = new CultureInfo("sma-NO");

            sb.AppendLine((info).DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            sb.AppendLine();

            info = new CultureInfo("ar-SA");

            sb.AppendLine((info).DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            sb.AppendLine();

            info = new CultureInfo("zh-MO");

            sb.AppendLine((info).DisplayName);

            sb.AppendLine(info.DisplayName + " Money: " +

(1234567.89).ToString("C", info));

            sb.AppendLine(info.DisplayName + " Date Time: " +

DateTime.Now.ToString(info));

            sb.AppendLine(info.DisplayName + " Numbers: " + (1234567.89).ToString(info));

 

            //foreach (CultureInfo inf in CultureInfo.GetCultures(CultureTypes.AllCultures))

            //    sb.AppendLine(inf.ToString());

            MessageBox.Show(sb.ToString());

 

The commented foreach loop, if uncommented, will display a list of all the supported Culture types.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: