Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / C#

Creating/Extending a Culture

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Mar 2010CPL5 min read 25.2K   4   2
Learn how to create/extend a culture and register it for future use (even by Windows)

Haven't you ever used a culture and wanted to extend it by changing e.g. the currency symbol? Haven't you ever wanted 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 a very tiny assembly. It contains only one class CultureAndRegionInfoBuilder and one enumeration CultureAndRegionModifiers that we will talk about 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.

C#
CultureAndRegionInfoBuilder builder =
    new CultureAndRegionInfoBuilder("en-US-NY",
        CultureAndRegionModifiers.None);

The constructor of CultureAndRegionInfoBuilder class requires two parameters. The first is the name of the new culture. The second can be 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 extend 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.

C#
CultureInfo parent = new CultureInfo("en-US");
builder.LoadDataFromCultureInfo(parent);
builder.LoadDataFromRegionInfo(new RegionInfo("US"));
builder.Parent = parent;

You might review the properties of both CultureInfo and RegionInfo and compare it to the properties of CultureAndRegionInfoBuilder to know exactly why we are loading both objects.

Now comes the hardest part, changing the properties to accommodate our needs and setting the name of the new culture.

C#
builder.RegionEnglishName = "New York";
builder.RegionNativeName = "New York";
builder.CultureEnglishName = "New York (United States)";
builder.CultureNativeName = "New York (United States)";
builder.NumberFormat.CurrencySymbol =  "*";

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:

C#
builder.Register();

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

Now, test your new culture:

C#
CultureInfo newYork = new CultureInfo("en-US-NY");
double money = 100.99;
Console.WriteLine(money.ToString("C", newYork));
// "C" means currency formatting

Congratulations! You created a new culture and you can see it in action.

There're some things that you need to take into account:

  1. You can unregister your created culture by using the static method Unregister of CultureAndRegionInfoBuilder class.
    C#
    CultureAndRegionInfoBuilder.Unregister("en-US-NY");
  2. After creating the desired culture and you don't want to register it immediately (for example) you can save it as XML and load it later.
    C#
    // Saving the culture
    builder.Save("D:New York Culture.xml");
    // Loading the saved culture
    CultureAndRegionInfoBuilder.CreateFromLdml
        ("D:New York Culture.xml");
  3. Windows saves the custom cultures you create in the folder %windir%\Globalization.
  4. You are not done 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.

Need more?

Links are subject to change. If you find a bad link on our site, please report it to us as soon as possible.

Posted in Globalization & Localization Tagged: .NET, CodeProject, CSharp, Globalization and Localization

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
BugSource code link invalid Pin
DD Naw Smith8-Sep-17 6:35
DD Naw Smith8-Sep-17 6:35 
BugSample source link is broken Pin
Júlio Nobre26-Nov-14 0:40
Júlio Nobre26-Nov-14 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.