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.
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.
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.
To use an alternate sort order, create an instance of the CultureInfo class with the locale identifier (LCID) in hexadecimal. The Hungarian technical sort LCID is 0x0001040e. Therefore, the CultureInfo instance is created as:
CultureInfo ci = new CultureInfo (0x0001040e);
One would then assign the instanced object to the CurrentCulture property. Sorting methods in .NET (for example, in a collection object) can then be used. The data will be sorted in the LCID format of the CultureInfo.LCID property.
Comparing Two Cultures
To compare values between two cultures, one has to ensure that any comparison is performed in either one or the other. There is also a comparison that may be performed in an invariant culture. To perform culture invariant comparisons, set the CurrentCulture property to CultureInfo.InvariantCulture.
A Quick Example: Culture Invariant Sort
We have a list of random strings that we wish to sort. The text file is in Norway. The strings are:
The sorted strings in the Norwegian culture (“nn-NO”) are:
The positions of the ü and ä characters are after the z character. The culture invariant strings are:
The culture invariant sort should be used when the sort results are compared to other cultures, for example, against Czech, French, Danish, Spanish or English.
Globalization allows the .NET Developer to sort, process and display data types in culture and locale specific formats. A good .NET design will incorporate globalization, especially if the product is outsourced to a foreign culture or locale. A globalization incorporated product will ensure that the end user is satisfied with the display, and that the product functions as required.
A PDF version of this technical article may be downloaded from the Coactum Solutions website at http://www.coactumsolutions.com/Articles.aspx.
Anupam Banerji has a wealth of experience writing, evaluating and validating systems for the cement, pharmaceuticals, energy derivatives and banking and finance industries.
Additionally, he’s designed and written software solutions in VB/VBA, SQL and C# .NET, and has worked as a Quantitative Analyst in banking. He is a Microsoft Certified Technology Specialist in WPF 3.5 and has written and deployed commercial solutions in the .NET 3.5 and 4.0 object models. He has also written applications in MVVM and Prism.