Tuesday, August 03, 2010

Globalization in C# .NET Assemblies

C# .NET assemblies are often built and deployed globally. The current business model of outsourcing development overseas results in global customers. The Developer implementing the outsourced solution must take local languages and customs into account. There are different ways of expressing characters, dates, times, currencies and floating point numbers in different cultures. In this article, Anupam Banerji explains the .NET approach towards globalization, and introduces the tools and methods to successfully implement globalization.
The software development business model today often results in the separation of the design and actual implementation in two separate countries. This will result in cultural differences. Dates and characters in Japan are expressed differently to those in the U.A.E. or Australia. A database in Spain holds currency information differently to that in the U.S. The Developer must be able to implement a foreign design that delivers the results expected overseas.
The .NET Framework provides us with objects to handle globalization issues. There are also several ways to compare and sort different cultural formats. Converting a Gregorian date to a Hijri date is a matter of a single call.
Culture & Locale Settings
There are two ways to set the culture in the .NET Framework. Both properties belong to the System.Threading namespace. They are Thread.CurrentThread.CurrentCulture and the Thread.CurrentThread. CurrentUICulture. The CurrentCulture property is set to calculate values of variables in a specific cultural format. The CurrentUICulture property determines the actual format that the value is displayed in.
To set the properties, we create an instance of the CultureInfo object.
using System.Globalization;
CultureInfo ci = new CultureInfo("en-US");
The string “en-US” tells the .NET runtime that the neutral culture is in English and that the locale, or region is the United States. The CultureInfo instance has a number of read only properties, which are used by the Developer to determine specific cultural settings.
Custom Cultures
A custom culture can be created by creating an instance of the CultureAndRegionInfo Builder property. The Register() method of this object instance should be called to save the culture to the registry. The example below shows a new culture created out of an existing culture:
CultureAndRegionInfoBuilder cb = new CultureAndRegionInfoBuilder("fr-AB", CultureAndRegionModifiers.None);
cb.LoadDataFromCultureInfo(new CultureInfo ("fr-FR"));
cb.LoadDataFromRegionInfo(new RegionInfo ("FR"));
This creates a new culture “fr-AB”, copying the cultural settings from French in France. To register the new culture, the assembly must be executed with administrative rights.
Sorting and Comparing Strings
String sorting is a key difference between cultures. Characters are treated differently in the sort order and cultures often support more than one sort order. For example, the German in Germany culture has two sort orders; one is the dictionary sort order and the other is the phonebook sort order. Hungarian has the default and technical sort orders, and Georgian has the traditional and the modern sort orders! The design should specify sort order requirements before being approved.
Read more: Codeproject