Creating Custom Cultures


Over time more and more languages are supported with the .NET Framework. However, not all languages of the world are available with .NET. You can create a custom culture. Some examples of where creating custom cultures can be useful are to support a minority within a region or to create subcultures for different dialects.

Custom cultures and regions can be created with the class CultureAndRegionInfoBuilder in the namespace System.Globalization. This class is located in the assembly sysglobl in the file sys-globl.dll.

With the constructor of the class CultureAndRegionInfoBuilder, you can pass the culture’s name. The second argument of the constructor requires an enumeration of type CultureAndRegionModifiers. This enumeration allows one of three values: Neutral for a neutral culture, Replacement if an existing Framework-culture should be replaced, or None.

After the CultureAndRegionInfoBuilder object is instantiated, you can configure the culture by setting properties. With the properties of this class, you can define all the cultural and regional information such as name, calendar, number format, metric information, and so on. If the culture should be based on existing cultures and regions, you can set the properties of the instance using the methods LoadDataFromCultureInfo() and LoadDataFromRegionInfo(), and change the values that are different by setting the properties afterward.

Calling the method Register() registers the new culture with the operating system. Indeed, you can find the file that describes the culture in the directory <windows>\Globalization. Look for files with the extension .nlp.

  // Create a Styria culture CultureAndRegionInfoBuilder styria = new CultureAndRegionInfoBuilder(       "de-AT-ST", CultureAndRegionModifiers.None); CultureInfo parent = new CultureInfo("de-AT"); styria.LoadDataFromCultureInfo(parent); styria.LoadDataFromRegionInfo(new RegionInfo("AT")); styria.Parent = parent; styria.RegionNativeName = "Steiermark"; styria.RegionEnglishName = "Styria"; styria.CultureEnglishName = "Styria (Austria)"; styria.CultureNativeName = "Steirisch"; styria.Register(); 

The newly created culture now can be used like other cultures:

  CultureInfo ci = new CultureInfo("de-AT-ST"); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; 

You can use the culture for formatting and also for resources. If you start the Culture Information application that was written earlier in this chapter again, you can see the custom culture as well.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net