How to: Create Custom Cultures

The predefined cultures provided with the .NET Framework and the Windows operating system specify the language and calendar used in a country/region, and the text conventions used to format, parse, and compare strings, dates, and numbers. However, you can create a custom culture if the predefined cultures do not provide the required information.

To define and create a custom culture

  1. Use a CultureAndRegionInfoBuilder object to define and name a custom culture. The custom culture can be an entirely new culture, a new culture based on an existing culture, or a culture that replaces an existing .NET Framework culture or Windows locale.

    For new cultures, you can populate the CultureAndRegionInfoBuilder object properties with culture information by calling the CultureAndRegionInfoBuilder.LoadDataFromCultureInfo method, and with region information by calling the CultureAndRegionInfoBuilder.LoadDataFromRegionInfo method. For replacement cultures, the CultureAndRegionInfoBuilder object properties are automatically populated from the properties of the culture that is being replaced.

  2. Modify the properties of the CultureAndRegionInfoBuilder object as necessary.

  3. Call the CultureAndRegionInfoBuilder.Register method to register the custom culture. The registration process performs these tasks:

    • Creates an .nlp file that contains the information defined in the CultureAndRegionInfoBuilder object.

    • Stores the .nlp file in the %windir%\Globalization system directory on your computer. This causes the culture information to persist on the computer even if the computer is turned off. You must have administrative privileges on this computer because the .nlp file is stored in a system directory.

    • Prepares the .NET Framework to search the %windir%\Globalization system directory instead of an internal cache the next time there is a request to create your new custom culture.

    Caution noteCaution

    If you do not have administrative privileges on the computer on which the custom culture is to be registered, the call to the CultureAndRegionInfoBuilder.Register method throws an UnauthorizedAccessException. You can work around this requirement by using reflection to call a private method. For more information, see How to: Save Custom Cultures Without Administrative Privileges.

    Your custom culture now has the same status as any predefined .NET Framework culture or Windows locale on your computer. The custom culture is available until a call to the CultureAndRegionInfoBuilder.Unregister method removes the .nlp file from the local computer.

  4. To instantiate a CultureInfo object based on the custom culture, specify its name in a CultureInfo class constructor.

Example

The following example uses the CultureAndRegionInfoBuilder class to define and register a custom culture named x-en-US-sample, and then constructs a CultureInfo object for the custom culture. The example also shows some corresponding properties of the CultureAndRegionInfoBuilder and CultureInfo objects.

' This example demonstrates the System.Globalization.Culture-
' AndRegionInfoBuilder Register method.
' Compile this code example with a reference to sysglobl.dll.

Imports System
Imports System.Globalization

Class Sample
    Public Shared Sub Main() 
        Dim cib As CultureAndRegionInfoBuilder = Nothing
        Try
            ' Create a CultureAndRegionInfoBuilder object named "x-en-US-sample".
            Console.WriteLine("Create and explore the CultureAndRegionInfoBuilder..." & vbCrLf)
            cib = New CultureAndRegionInfoBuilder("x-en-US-sample", CultureAndRegionModifiers.None)

            ' Populate the new CultureAndRegionInfoBuilder object with culture information.
            Dim ci As New CultureInfo("en-US")
            cib.LoadDataFromCultureInfo(ci)

            ' Populate the new CultureAndRegionInfoBuilder object with region information.
            Dim ri As New RegionInfo("US")
            cib.LoadDataFromRegionInfo(ri)

            ' Display some of the properties of the CultureAndRegionInfoBuilder object.
            Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName)
            Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName)
            Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName)
            Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId)
            Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric)
            Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol)
            Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName)
            Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName)
            Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName)
            Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName)
            Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName)
            Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName)
            Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName)
            Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName)
            Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName)
            Console.WriteLine()

            ' Register the custom culture.
            Console.WriteLine("Register the custom culture...")
            cib.Register()

            ' Display some of the properties of the custom culture.
            Console.WriteLine("Create and explore the custom culture..." & vbCrLf)
            ci = New CultureInfo("x-en-US-sample")

            Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name)
            Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName)
            Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName)
            Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName)
            Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName)
            Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName)

            Console.WriteLine(vbCrLf & "Note:" & vbCrLf & "Use the example in the " & _
                              "Unregister method topic to remove the custom culture.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

    End Sub 'Main
End Class 'Sample

'This code example produces the following results:
'
'Create and explore the CultureAndRegionInfoBuilder...
'
'CultureName:. . . . . . . . . . x-en-US-sample
'CultureEnglishName: . . . . . . English (United States)
'CultureNativeName:. . . . . . . English (United States)
'GeoId:. . . . . . . . . . . . . 244
'IsMetric: . . . . . . . . . . . False
'ISOCurrencySymbol:. . . . . . . USD
'RegionEnglishName:. . . . . . . United States
'RegionName: . . . . . . . . . . x-en-US-sample
'RegionNativeName: . . . . . . . United States
'ThreeLetterISOLanguageName: . . eng
'ThreeLetterISORegionName: . . . USA
'ThreeLetterWindowsLanguageName: ENU
'ThreeLetterWindowsRegionName: . USA
'TwoLetterISOLanguageName: . . . en
'TwoLetterISORegionName: . . . . US
'
'Register the custom culture...
'Create and explore the custom culture...
'
'Name: . . . . . . . . . . . . . x-en-US-sample
'EnglishName:. . . . . . . . . . English (United States)
'NativeName: . . . . . . . . . . English (United States)
'TwoLetterISOLanguageName: . . . en
'ThreeLetterISOLanguageName: . . eng
'ThreeLetterWindowsLanguageName: ENU
'
'Note:
'Use the example in the Unregister method topic to remove the custom culture.
'
// This example demonstrates the System.Globalization.Culture-
// AndRegionInfoBuilder Register method.
// Compile this code example with a reference to sysglobl.dll.

using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    CultureAndRegionInfoBuilder cib = null;
    try 
    {
// Create a CultureAndRegionInfoBuilder object named "x-en-US-sample".
    Console.WriteLine("Create and explore the CultureAndRegionInfoBuilder...\n");
    cib = new CultureAndRegionInfoBuilder(
                         "x-en-US-sample", CultureAndRegionModifiers.None);

// Populate the new CultureAndRegionInfoBuilder object with culture information.
    CultureInfo ci = new CultureInfo("en-US");
    cib.LoadDataFromCultureInfo(ci);

// Populate the new CultureAndRegionInfoBuilder object with region information.
    RegionInfo  ri = new RegionInfo("US");
    cib.LoadDataFromRegionInfo(ri);

// Display some of the properties of the CultureAndRegionInfoBuilder object.
    Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName);
    Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName);
    Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName);
    Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId);
    Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric);
    Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol);
    Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName);
    Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName);
    Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName);
    Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName);
    Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName);
    Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName);
    Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName);
    Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName);
    Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName);
    Console.WriteLine();

// Register the custom culture.
    Console.WriteLine("Register the custom culture...");
    cib.Register();

// Display some of the properties of the custom culture.
    Console.WriteLine("Create and explore the custom culture...\n");
    ci = new CultureInfo("x-en-US-sample");

    Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name);
    Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName);
    Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName);
    Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName);
    Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName);
    Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName);

    Console.WriteLine("\nNote:\n" +
        "Use the example in the Unregister method topic to remove the custom culture.");
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    }
}
/*
This code example produces the following results:

Create and explore the CultureAndRegionInfoBuilder...

CultureName:. . . . . . . . . . x-en-US-sample
CultureEnglishName: . . . . . . English (United States)
CultureNativeName:. . . . . . . English (United States)
GeoId:. . . . . . . . . . . . . 244
IsMetric: . . . . . . . . . . . False
ISOCurrencySymbol:. . . . . . . USD
RegionEnglishName:. . . . . . . United States
RegionName: . . . . . . . . . . x-en-US-sample
RegionNativeName: . . . . . . . United States
ThreeLetterISOLanguageName: . . eng
ThreeLetterISORegionName: . . . USA
ThreeLetterWindowsLanguageName: ENU
ThreeLetterWindowsRegionName: . USA
TwoLetterISOLanguageName: . . . en
TwoLetterISORegionName: . . . . US

Register the custom culture...
Create and explore the custom culture...

Name: . . . . . . . . . . . . . x-en-US-sample
EnglishName:. . . . . . . . . . English (United States)
NativeName: . . . . . . . . . . English (United States)
TwoLetterISOLanguageName: . . . en
ThreeLetterISOLanguageName: . . eng
ThreeLetterWindowsLanguageName: ENU

Note:
Use the example in the Unregister method topic to remove the custom culture.

*/

See Also

Reference

CultureInfo

CultureAndRegionInfoBuilder

CultureAndRegionModifiers

Other Resources

Encoding and Localization