Whidbey .Net 2.0 Custom Culture Example (Beta 2)

Here’s an example of how to create a custom culture in Whidbey. Its changed a bit from earlier versions, so this code is based on Beta 2 behavior. This should be what you see in RTM. J

This sample creates a custom culture with the name “en-US-shawn”, changes a couple things and then uses that culture to display that we did indeed get custom behavior. It then unregisters it. Copy the example to something like cultures.cs and then use

            csc /r:sysglobl.dll cultures.cs

            cultures.exe

Output is something like:

Data from our new culture:

en-US-shawn

Shawn's version of en-US

3/8/2005 17:33:50 PM (shawn)

Data from our new culture's parent:

en-US

English (United States)

3/8/2005 5:33 PM

This WILL NOT compile on previous prerelease versions, so be patient J  I had questions though so wanted people to be able to see how the custom culture mechanism worked. If for some reason this culture doesn’t unregister, delete the %windir%globalizationen-us-shawn.nlp file by hand.

// This posting is provided "AS IS" with no warranties, and confers no rights. Use of included samples are subject to the terms specified at https://www.microsoft.com/ info/cpyright.htm

using System;

using System.Globalization;

// Example of creating a custom culture.

namespace cultures

{

    public class mycultures

    {

        static void Main(string[] args)

        {

            // First we need a CultureAndRegionInfoBuilder

           

            // Base it on en-US, add my name

            CultureAndRegionInfoBuilder carib = new CultureAndRegionInfoBuilder(

                "en-US-shawn", CultureAndRegionModifiers.None);

            // Our parent is en-US, load those defaults

         CultureInfo enUS = new CultureInfo("en-US");

            carib.LoadDataFromCultureInfo(enUS);

            carib.LoadDataFromRegionInfo(new RegionInfo("en-US"));

            carib.Parent=enUS;

            // Override a few things so we know we changed it

            carib.CultureEnglishName = carib.CultureNativeName = "Shawn's version of en-US";

            // Date time is a little more challenging to override.

            DateTimeFormatInfo dtfi = new CultureInfo("en-US").DateTimeFormat;

            dtfi.ShortTimePattern = "HH:mm:ss tt' (shawn)'";

            carib.GregorianDateTimeFormat = dtfi;

            // Register it

            carib.Register();

            // Now try to use it

            Console.WriteLine("Data from our new culture:");

      CultureInfo enUsShawn = new CultureInfo("en-US-shawn");

            Console.WriteLine(enUsShawn.Name);

            Console.WriteLine(enUsShawn.EnglishName);

            Console.WriteLine(DateTime.Now.ToString("g", enUsShawn));

            Console.WriteLine();

            // Compare that to our parent

            Console.WriteLine("Data from our new culture's parent:");

            enUS = enUsShawn.Parent;

            Console.WriteLine(enUS.Name);

            Console.WriteLine(enUS.EnglishName);

            Console.WriteLine(DateTime.Now.ToString("g", enUS));

           

            // Now we're done with it

            CultureAndRegionInfoBuilder.Unregister("en-US-shawn");

        }

    }

}