Category Archives: Globalization

Haven’t you ever used a culture and want to extend it by changing e.g. the currency symbol? Haven’t you ever want to create a custom culture for formatting purposes? Answers will vary depending on the developer and the users targeted by his application.

Overtime, more and more cultures are started to be supported by the .NET Framework. However, not all cultures of the world are available in .NET. If you want to use a culture that is not available or you want to support a minority with a region you will need to create your custom culture.

Custom cultures and regions can be created with the class System.Globalization.CultureAndRegionInfoBuilder that resides in the assembly sysglobl.dll (you will need to reference it of course.)

Now, we are going to create a custom culture that extends the U.S. English culture. This culture is for New York. We’ll step further and change some of the characteristics of the base culture (en-US) to accommodate our needs for a good example.

We’ll start by referencing the assembly sysglobl.dll and add a using statement for the namespace System.Globalization.

sysglobl.dll is very tiny assembly. It contains only one class CultureAndRegionInfoBuilder and one enumeration CultureAndRegionModifiers that we will talk about it soon.

Next, we will instantiate a new instance of CultureAndRegionInfo class. That class is the key class for creating/extending a culture. Plus, it allows you to register/unregister your â€œbaby.â€ It is required to register your custom culture on the system before you start using it.

The constructor of CultureAndRegionInfoBuilder class requires two parameters. The first is the name of the new culture. The second can one of the three values defined by the CultureAndRegionModifiers enumeration. And that parameter is used to identify the new culture.

The values of CultureAndRegionModifiers are:

None:
For new cultures and the cultures that extends existing ones.

Neutral:
For neutral cultures such as English, French, etc.

Replacement:
For the cultures that you intend to be a replacement of existing culture in .NET Framework or even Windows. A replacement culture is like en-US that would replace the existing culture English (United States.)

In cultures world, there’s a unique name for every culture. And that name follows the naming convention xx-XX. The first lowercase xx is for language like en (English,) fr (French,) es (Spanish,) and so on. The second uppercase XX is for the country/region like US, FR, and ES. So an example like de-DE is the unique name German in Germany.

There’re exceptions to these rules. First, thereâ€™re languages like Dhivehi and Konkani that are abbreviated to three letters like div and kok. In addition, there’re two neutral cultures zh-CHS and zh-CHT for Simplified and Traditional Chinese; those have three letters for the country. In addition, there’re cultures that have the suffix -Cyrl and -Latn for defining the script Cyrillic or Latin. See RFC 1766 Standards.

After instantiating a new object of type CultureAndRegionInfoBuilder you need to initialize its properties with valid values. For the example in our hands, we will load the values from the parent culture en-US because we intend to extend it. Actually, there’s no rules to admit in loading the values for the new culture. You can load it from existing culture or you can write it yourself. In addition, we will set the Parent property to the parent culture that we wish to extend.

In the last few lines we changed the region â€œEnglishâ€ and its native name â€œEnglishâ€ to â€œNew Yorkâ€.

In addition, we changed the currency symbol from the dollar sign $ to an asterisk *.

Notice the difference between the English name and native name. The native name is the name of the culture in the native language that the culture is supposed to support. For example, â€œFrenchâ€ is the English name is and native name is “franÃ§ais”.

Here comes the hottest part, registering your new culture:

builder.Register();

You might get an exception of type InvalidOperationException if you tried to re-register it or if you chose a name that’s existed and you are not creating a replacement culture.

Windows saves the custom cultures you create in the folder %windir%Globalization.

You are not ended using the custom culture in your .NET code only. You can go to Regional and Language Settings in Control Panel and change the culture to your created one and that affects the entire system.

Comparing Strings

Sometimes, you will be faced with a situation where you need to compare two identical strings differ only by their diacritics (combining characters) for instance. If you were to compare them using the common way (using String.Compare for instance) they would be different because of the combining characters.

To overcome this you will need to use a special overload of String.Compare method:

The Kashida, isn’t of the Arabic alphabets. It’s most likely be a space! So the option CompareOptions.IgnoreSymbols ignores it from comparison.