Home > Articles > Programming > Windows Programming

This chapter is from the book

Supplemental Substitute Custom Cultures

A "supplemental substitute" custom culture certainly sounds like a contradiction in terms. I use this term to describe a supplemental custom culture that exists to replace an existing culture without actually replacing it. In the "Public Custom Cultures and Naming Conventions" section, I discussed the problems with replacement custom cultures and suggested a solution in which, instead of creating a replacement custom culture, a new supplemental custom culture could be created that was in every way like the intended replacement custom culture. Creating a new custom culture that is like an existing custom culture is made easy with the LoadDataFromCultureInfo and LoadDataFromRegionInfo methods. This is the code for creating an en-GB-Acme supplemental substitute custom culture:

CultureInfo cultureInfo = new CultureInfo("en-GB");
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

CultureAndRegionInfoBuilder builder =
    new CultureAndRegionInfoBuilder("en-GB-Acme",
    CultureAndRegionModifiers.None);

// load in the data from the existing culture and region
builder.LoadDataFromCultureInfo(cultureInfo);
builder.LoadDataFromRegionInfo(regionInfo);

// make custom changes to the culture
builder.GregorianDateTimeFormat.ShortTimePattern = "HH:mm tt";

builder.Register();

The LoadDataFromCultureInfo and LoadDataFromRegionInfo methods set CultureAndRegionInfoBuilder properties from the data in the CultureInfo and RegionInfo objects, respectively. Tables 11.3 and 11.4 show the properties set by these methods.

Table 11.3. Properties Set by CultureAndRegionInfoBuilder. LoadDataFromCultureInfo

CultureAndRegionInfoBuilder Property

Source

AvailableCalendars

CultureInfo.OptionalCalendars (specific cultures only)

CompareInfo

CultureInfo.CompareInfo (supplemental only)

ConsoleFallbackUICulture

CultureInfo.GetConsoleFallbackUICulture()

CultureEnglishName

CultureInfo.EnglishName

CultureNativeName

CultureInfo.NativeName

GregorianDateTimeFormat

CultureInfo.DateTimeFormat (specific cultures only)

IetfLanguageTag

CultureInfo.IetfLanguageTag

IsRightToLeft

CultureInfo.TextInfo.IsRightToLeft

KeyboardLayoutId

CultureInfo.KeyboardLayoutId

NumberFormat

CultureInfo.NumberFormat (specific cultures only)

Parent

CultureInfo.Parent

TextInfo

CultureInfo.TextInfo (supplemental only)

ThreeLetterISOLanguageName

CultureInfo.ThreeLetterISOLanguageName

ThreeLetterWindowsLanguageName

CultureInfo.ThreeLetterWindowsLanguageName (supplemental only)

TwoLetterISOLanguageName

CultureInfo.TwoLetterISOLanguageName

Table 11.4. Properties Set by CultureAndRegionInfoBuilder.LoadDataFrom RegionInfo

CultureAndRegionInfoBuilder Property

Source

CurrencyEnglishName

RegionInfo.CurrencyEnglishName

CurrencyNativeName

RegionInfo.CurrencyNativeName

GeoId

RegionInfo.GeoId

IsMetric

RegionInfo.IsMetric

ISOCurrencySymbol

RegionInfo.ISOCurrencySymbol

RegionEnglishName

RegionInfo.EnglishName

RegionNativeName

RegionInfo.NativeName

ThreeLetterISORegionName

RegionInfo.ThreeLetterISORegionName

ThreeLetterWindowsRegionName (supplemental only)

RegionInfo.ThreeLetterWindowsRegionName

TwoLetterISORegionName

RegionInfo.TwoLetterISORegionName

Notice that the CompareInfo, TextInfo, ThreeLetterWindowsLanguageName, and ThreeLetterWindowsRegionName properties are set by these methods only if the culture is a supplemental culture (which, in this example, it is). For replacement cultures, these properties are set in the CultureAndRegionInfoBuilder constructor and are considered immutable. Consequently, if you assign values to these properties for replacement cultures, they will throw an exception. This is why you can't create a replacement custom culture that simply changes the default sort order. This code attempts to create a replacement culture for es-ES (Spanish (Spain)) when the only difference is that the sort order is Traditional (0x0000040A) instead of the default International:

CultureAndRegionInfoBuilder builder =
    new CultureAndRegionInfoBuilder("es-ES",
    CultureAndRegionModifiers.Replacement);
    builder.CompareInfo = CompareInfo.GetCompareInfo(0x0000040A);

    builder.Register();

The assignment to CompareInfo throws a NotSupportedException. Therefore, a benefit of using a supplemental custom culture instead of a replacement culture is that these properties can have different values than those of the original culture.

In addition to the public properties in Table 11.3 the LoadDataFromCultureInfo method sets internal values for DurationFormats, FontSignature, and PaperSize. These values are used in the LDML file created by the Save method. The LoadDataFromCultureInfo method represents the only way to set these properties.

The resulting supplemental custom culture does not have the complete functionality of the replacement custom culture. One difference lies in the behavior of the CultureInfo.DisplayName property. This property has a certain level of intelligence built into it. The DisplayName property returns the name of the culture for the CurrentCulture for built-in .NET Framework and Windows cultures. This means that the DisplayName for the fr-FR culture is "French (France)" when the CurrentCulture is "en-US", but it is "Français (France)" and "Französisch (Frankreich)" when the CurrentCulture is "fr-FR" and "de-DE", respectively, and the French and German .NET Framework Language Packs have been installed. Replacement cultures adopt the same functionality because the .NET Framework can identify that the culture is known. The same functionality is not available to supplemental custom cultures because the .NET Framework cannot and should not guess at the correct DisplayName. Consequently, the DisplayName of a supplemental custom culture is the same as the native name. Table 11.5 shows the difference in behavior for a tr-TR (Turkish (Turkey)) custom culture.

Table 11.5. CultureInfo.DisplayName Behavioral Difference for Replacement and Supplemental Custom Cultures

CurrentCulture

tr-TR Replacement Culture DisplayName

tr-TR Supplemental Culture DisplayName

en-US

Turkish (Turkey)

Türkçe (Türkiye)

tr-TR

Türkçe (Türkiye)

Türkçe (Türkiye)

The same difference in behavior is true for RegionInfo.DisplayName.

  • + Share This
  • 🔖 Save To Your Account