< C++ .NET CultureInfo Class Info 1 | Main | C++ .NET Loop and Decision 1 >


 

Supplementary Note:

CultureInfo Class Information 2

 

The following are the topics available in this part.

  1. Invariant, Neutral, and Specific Cultures

  2. Windows Locales

  3. Control Panel Overrides

  4. Alternate Sort Orders

  5. Implemented Interfaces & Examples

 

 

Invariant, Neutral, and Specific Cultures

 

The cultures are generally grouped into three sets: the invariant culture, the neutral cultures, and the specific cultures. The invariant culture is culture-insensitive. You can specify the invariant culture by name using an empty string ("") or by its culture identifier 0x007F. InvariantCulture retrieves an instance of the invariant culture. It is associated with the English language but not with any country/region. It can be used in almost any method in the Globalization namespace that requires a culture. A neutral culture is a culture that is associated with a language but not with a country/region. A specific culture is a culture that is associated with a language and a country/region. For example, "fr" is a neutral culture and "fr-FR" is a specific culture. Note that "zh-CHS" (Simplified Chinese) and "zh-CHT" (Traditional Chinese) are neutral cultures. The cultures have a hierarchy, such that the parent of a specific culture is a neutral culture and the parent of a neutral culture is the InvariantCulture. The Parent property returns the neutral culture associated with a specific culture. If the resources for the specific culture are not available in the system, the resources for the neutral culture are used; if the resources for the neutral culture are not available, the resources embedded in the main assembly are used.

The list of cultures in the Windows API is slightly different from the list of cultures in the .NET Framework. For example, the neutral culture zh-CHT "Chinese (Traditional)" with culture identifier 0x7C04 is not available in the Windows API. If interoperability with Windows is required (for example, through the p/invoke mechanism), use a specific culture that is defined in the operating system. This will ensure consistency with the equivalent Windows locale, which is identified with the same LCID. A DateTimeFormatInfo or a NumberFormatInfo can be created only for the invariant culture or for specific cultures, not for neutral cultures. If DateTimeFormatInfo.Calendar is the TaiwanCalendar but the Thread.CurrentCulture is not "zh-TW", then DateTimeFormatInfo.NativeCalendarName, DateTimeFormatInfo.GetEraName, and DateTimeFormatInfo.GetAbbreviatedEraName return an empty string ("").

 

Windows Locales

 

Starting in the .NET Framework version 2.0, the CultureInfo constructor supports using Windows Locales, which are equivalent to cultures, to automatically generate cultures that do not exist in the .NET Framework.

 

Control Panel Overrides

 

The user might choose to override some of the values associated with the current culture of Windows through Regional and Language Options (or Regional Options or Regional Settings) in Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If UseUserOverride is true and the specified culture matches the current culture of Windows, the CultureInfo uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. If the user settings are incompatible with the culture associated with the CultureInfo (for example, if the selected calendar is not one of the OptionalCalendars), the results of the methods and the values of the properties are undefined. For cultures that use the euro, the .NET Framework and Windows XP set the default currency as euro; however, older versions of Windows do not. Therefore, if the user of an older version of Windows has not changed the currency setting through Regional Options or Regional Settings in Control Panel, the currency might be incorrect. To use the .NET Framework default setting for the currency, use a CultureInfo constructor overload that accepts a useUserOverride parameter and set it to false.

 

Alternate Sort Orders

 

The culture identifier "0x0c0a" for "Spanish - Spain" uses the default international sort order; the culture identifier "0x040A", which is also for "Spanish - Spain", uses the traditional sort order. If the CultureInfo is constructed using the "es-ES" culture name, the new CultureInfo uses the default international sort order. To construct a CultureInfo that uses the traditional sort order, use the culture identifier "0x040A" with the constructor.

 

Implemented Interfaces & Examples

 

This class implements the ICloneable interface to enable duplication of CultureInfo objects. It also implements IFormatProvider to supply formatting information to applications. The following code example shows how to create a CultureInfo for “Spanish - Spain " with the international sort and another CultureInfo with the traditional sort.

// testprog.cpp : main project file.

// CultureInfo example

#include "stdafx.h"

 

using namespace System;

using namespace System::Collections;

using namespace System::Globalization;

 

int main()

{

      // Creates and initializes the CultureInfo which uses the international sort.

      CultureInfo^ myCIintl = gcnew CultureInfo("es-ES", false);

      // Creates and initializes the CultureInfo which uses the traditional sort.

      CultureInfo^ myCItrad = gcnew CultureInfo(0x040A, false);

      // Displays the properties of each culture.

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL");

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "========", "=============", "===========");

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl->CompareInfo, myCItrad->CompareInfo);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl->DisplayName, myCItrad->DisplayName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl->EnglishName, myCItrad->EnglishName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl->IsNeutralCulture, myCItrad->IsNeutralCulture);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl->IsReadOnly, myCItrad->IsReadOnly);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "LCID", myCIintl->LCID, myCItrad->LCID);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "Name", myCIintl->Name, myCItrad->Name);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl->NativeName, myCItrad->NativeName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "Parent", myCIintl->Parent, myCItrad->Parent);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl->TextInfo, myCItrad->TextInfo);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl->ThreeLetterISOLanguageName, myCItrad->ThreeLetterISOLanguageName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl->ThreeLetterWindowsLanguageName, myCItrad->ThreeLetterWindowsLanguageName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl->TwoLetterISOLanguageName, myCItrad->TwoLetterISOLanguageName);

      Console::WriteLine();

      // Compare two strings using myCIintl...

      Console::WriteLine("Comparing \"llegar\" and \"lugar\"" );

      Console::WriteLine("   With myCIintl->CompareInfo->Compare: {0}", myCIintl->CompareInfo->Compare("llegar", "lugar"));

      Console::WriteLine("   With myCItrad->CompareInfo->Compare: {0}", myCItrad->CompareInfo->Compare("llegar", "lugar"));

 

      return 0;

}

 

Output:

 

 

 

 

 

 

 

--------------------------------------------------------------------------------------------------------

C++ .NET formatting - code example that shows how to create a CultureInfo for “Spanish - Spain " with the international sort and another CultureInfo with the traditional sort

 

The following code example determines the parent culture of each specific culture using the Chinese language.

// testprog.cpp : main project file.

// CultureInfo - specific and parent culture

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

 

int main()

{

  // Prints the header.

  Console::WriteLine("SPECIFIC CULTURE                                  PARENT CULTURE");

  Console::WriteLine("================                                  ==============");

  // Determines the specific cultures that use the Chinese language,

  // and displays the parent culture.

  System::Collections::IEnumerator^ en = CultureInfo::GetCultures( CultureTypes::SpecificCultures )->GetEnumerator();

 

  while (en->MoveNext())

  {

     CultureInfo^ ci = safe_cast<CultureInfo^>(en->Current);

     if (ci->TwoLetterISOLanguageName->Equals("zh"))

     {

        Console::Write("0x{0} {1} {2,-37}", ci->LCID.ToString("X4"), ci->Name, ci->EnglishName);

        Console::WriteLine("0x{0} {1} {2}", ci->Parent->LCID.ToString("X4"), ci->Parent->Name, ci->Parent->EnglishName);

     }

  }

 return 0;

}

 

Output:

C++ .NET formatting - code example that determines the parent culture of each specific culture using the Chinese language

 

 

 

 

 

Part 1 | Part 2


< C++ .NET CultureInfo Class Info 1 | Main | C++ .NET Loop and Decision 1 >