Export (0) Print
Expand All
9 out of 12 rated this helpful - Rate this topic

CultureInfo Class

Provides information about a specific culture (called a locale for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, and formatting for dates and sort strings.

System.Object
  System.Globalization.CultureInfo

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class CultureInfo : ICloneable, 
	IFormatProvider

The CultureInfo type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkCultureInfo(Int32)Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCultureInfo(String)Initializes a new instance of the CultureInfo class based on the culture specified by name.
Public methodSupported by the XNA FrameworkCultureInfo(Int32, Boolean)Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier and on the Boolean that specifies whether to use the user-selected culture settings from the system.
Public methodSupported by the XNA FrameworkCultureInfo(String, Boolean)Initializes a new instance of the CultureInfo class based on the culture specified by name and on the Boolean that specifies whether to use the user-selected culture settings from the system.
Top
  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCalendarGets the default calendar used by the culture.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCompareInfoGets the CompareInfo that defines how to compare strings for the culture.
Public propertyCultureTypesGets the culture types that pertain to the current CultureInfo object.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentCultureGets the CultureInfo that represents the culture used by the current thread.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentUICultureGets the CultureInfo that represents the current culture used by the Resource Manager to look up culture-specific resources at run time.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryDateTimeFormatGets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
Public propertyDisplayNameGets the culture name in the format "<languagefull> (<country/regionfull>)" in the language of the localized version of .NET Framework.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryEnglishNameGets the culture name in the format "<languagefull> (<country/regionfull>)" in English.
Public propertyIetfLanguageTagDeprecated. Gets the RFC 4646 standard identification for a language.
Public propertyStatic memberInstalledUICultureGets the CultureInfo that represents the culture installed with the operating system.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInvariantCultureGets the CultureInfo object that is culture-independent (invariant).
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryIsNeutralCultureGets a value indicating whether the current CultureInfo represents a neutral culture.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryIsReadOnlyGets a value indicating whether the current CultureInfo is read-only.
Public propertyKeyboardLayoutIdGets the active input locale identifier.
Public propertySupported by the XNA FrameworkLCIDGets the culture identifier for the current CultureInfo.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNameGets the culture name in the format "languagecode2-country/regioncode2".
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNativeNameGets the culture name, consisting of the language, the country/region, and the optional script, that the culture is set to display.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberFormatGets or sets a NumberFormatInfo that defines the culturally appropriate format of displaying numbers, currency, and percentage.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryOptionalCalendarsGets the list of calendars that can be used by the culture.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryParentGets the CultureInfo that represents the parent culture of the current CultureInfo.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTextInfoGets the TextInfo that defines the writing system associated with the culture.
Public propertySupported by the XNA FrameworkThreeLetterISOLanguageNameGets the ISO 639-2 three-letter code for the language of the current CultureInfo.
Public propertySupported by the XNA FrameworkThreeLetterWindowsLanguageNameGets the three-letter code for the language as defined in the Windows API.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTwoLetterISOLanguageNameGets the ISO 639-1 two-letter code for the language of the current CultureInfo.
Public propertySupported by the XNA FrameworkUseUserOverrideGets a value indicating whether the current CultureInfo uses the user-selected culture settings.
Top
  NameDescription
Public methodSupported by the XNA FrameworkClearCachedDataRefreshes cached culture-related information.
Public methodSupported by the XNA FrameworkCloneCreates a copy of the current CultureInfo.
Public methodStatic memberSupported by the XNA FrameworkCreateSpecificCultureCreates a CultureInfo that represents the specific culture that is associated with the specified name.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsDetermines whether the specified object is the same culture as the current CultureInfo. (Overrides Object.Equals(Object).)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetConsoleFallbackUICultureGets an alternate user interface culture suitable for console applications when the default graphic user interface culture is unsuitable.
Public methodStatic memberSupported by the XNA FrameworkGetCultureInfo(Int32)Retrieves a cached, read-only instance of a culture by using the specified culture identifier.
Public methodStatic memberSupported by the XNA FrameworkGetCultureInfo(String)Retrieves a cached, read-only instance of a culture by using the specified culture name.
Public methodStatic memberGetCultureInfo(String, String)Retrieves a cached, read-only instance of a culture. Parameters specify a culture that is initialized with the TextInfo and CompareInfo objects specified by another culture.
Public methodStatic memberGetCultureInfoByIetfLanguageTagDeprecated. Retrieves a read-only CultureInfo object having linguistic characteristics that are identified by the specified RFC 4646 language tag.
Public methodStatic memberGetCulturesGets the list of supported cultures filtered by the specified CultureTypes parameter.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetFormatGets an object that defines how to format the specified type.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeServes as a hash function for the current CultureInfo, suitable for hashing algorithms and data structures, such as a hash table. (Overrides Object.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadOnlyReturns a read-only wrapper around the specified CultureInfo.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string containing the name of the current CultureInfo in the format "languagecode2-country/regioncode2". (Overrides Object.ToString().)
Top

The CultureInfo class renders culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions. This class also provides access to culture-specific instances of DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo. These objects contain the information required for culture-specific operations, such as casing, formatting dates and numbers, and comparing strings.

The String class indirectly uses this class to obtain information about the default culture.

Culture Names and Identifiers

The CultureInfo class specifies a unique name for each culture, based on RFC 4646 (Windows Vista and later). The name is a combination of an ISO 639 two-letter lowercase culture code associated with a language and an ISO 3166 two-letter uppercase subculture code associated with a country or region.

NoteNote

When passed to a class constructor or a method such as CreateSpecificCulture or GetCultureInfo, the case of a culture name is not significant.

The format for the culture name is languagecode2-country/regioncode2, where languagecode2 is the language code and country/regioncode2 is the subculture code. Examples include ja-JP for Japanese (Japan) and en-US for English (United States). In cases where a two-letter language code is not available, a three-letter code derived from ISO 639-2 is used.

Note that some culture names also specify an ISO 15924 script. For example, "-Cyrl" specifies the Cyrillic script and "-Latn" specifies the Latin script. On Windows Vista and later, a culture name that includes a script is rendered by using the pattern languagecode2-scripttag-country/regioncode2. An example of this type of culture name is uz-Cyrl-UZ for Uzbek (Cyrillic, Uzbekistan). On Windows operating system versions before Windows Vista, a culture name including a script is rendered using the pattern languagecode2-country/regioncode2-scripttag; for example, uz-UZ-Cyrl for Uzbek (Cyrillic, Uzbekistan).

A neutral culture is specified by only the two-letter lowercase language code. For example, "fr" specifies the neutral culture for French, and "de" specifies the neutral culture for German.

NoteNote

There are two culture names that contradict this rule. The cultures Chinese (Simplified), named zh-Hans, and Chinese (Traditional), named zh-Hant, are neutral cultures. The culture names represent the current standard and should be used unless you have a reason for using the older names "zh-CHS" and "zh-CHT".

A culture identifier is a standard international numeric abbreviation and has the components necessary to uniquely identify one of the installed cultures. Your application can use predefined culture identifiers or define custom identifiers.

Certain predefined culture names and identifiers are used by this and other classes in the System.Globalization namespace. Detailed culture information appears in the National Language Support (NLS) API Reference at the Go Global Developer Center.

Remember that the culture names and identifiers represent only a subset of cultures that can be found on a particular computer. Windows versions or service packs can change the available cultures. Applications add custom cultures using the CultureAndRegionInfoBuilder class. Users add their own custom cultures using the Microsoft Locale Builder tool. Microsoft Locale Builder is written in managed code using the CultureAndRegionInfoBuilder class.

Several distinct names are closely associated with a culture, notably the names associated with the following class members:

See Names Associated with a CultureInfo Object for a discussion of the relationship among these names.

Invariant, Neutral, and Specific Cultures

The cultures are generally grouped into three sets: invariant cultures, neutral cultures, and specific cultures.

An invariant culture is culture-insensitive. Your application specifies the invariant culture by name using an empty string ("") or by its identifier. InvariantCulture defines an instance of the invariant culture. It is associated with the English language but not with any country/region. It is 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 the neutral name for the French culture, and fr-FR is the name of the specific French (France) culture. Note that Chinese (Simplified) and Chinese (Traditional) are also considered neutral cultures.

Creating an instance of a CompareInfo class for a neutral culture is not recommended because the data it contains is arbitrary. To display and sort data, specify both the language and region. Additionally, the Name property of a CompareInfo object created for a neutral culture returns only the country and does not include the region.

The defined cultures have a hierarchy in which the parent of a specific culture is a neutral culture and the parent of a neutral culture is the invariant culture. The Parent property contains the neutral culture associated with a specific culture. Custom cultures should define the Parent property in conformance with this pattern.

If the resources for a specific culture are not available in the operating system, the resources for the associated neutral culture are used. If the resources for the neutral culture are not available, the resources embedded in the main assembly are used. For more information on the resource fallback process, see Packaging and Deploying Resources.

The list of locales in the Windows API is slightly different from the list of cultures supported by the .NET Framework. If interoperability with Windows is required, for example, through the p/invoke mechanism, the application should use a specific culture that is defined for the operating system. Use of the specific culture ensures consistency with the equivalent Windows locale, which is identified with a locale identifier that is the same as 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 set to "zh-TW", then DateTimeFormatInfo.NativeCalendarName, DateTimeFormatInfo.GetEraName, and DateTimeFormatInfo.GetAbbreviatedEraName return an empty string ("").

Custom Cultures

In addition to the predefined cultures supported by the Windows operating system and the .NET Framework, the .NET Framework supports three types of custom cultures:

  • New cultures that supplement the cultures available in Windows or the .NET Framework. For example, an application could install a CultureInfo object that represents the fj-FJ (or Fijan (Fiji)) culture on a system.

  • Replacement cultures whose properties are different from the properties of the standard cultures supported by Windows and the .NET Framework.

  • Standard cultures with user overrides. The user can use the Region and Language application in Control Panel to customize an existing culture's property values.

NoteNote

You can use the CultureAndRegionInfoBuilder class to define, save, and register custom cultures that either supplement or replace existing cultures. The CultureAndRegionInfoBuilder.Save method creates a Locale Data Markup Language (LDML) file that can be used to install a custom culture on target systems.

Because the .NET Framework supports custom cultures, you should consider the following when working with culture-specific data:

  • Custom cultures can have values that exceed the ranges of the predefined cultures. For example, some cultures have unusually long month names, unexpected date or time formats, or other unusual data.

  • When you display culture-specific data in the user interface, you should respect the user's customizations; for example, the user might want a 24-hour clock or a yyyyMMdd date format.

  • Remember that custom cultures override default values. Therefore, you cannot consider culture data to be stable. Country names, number and date formats, and spellings may change in the future. If you want to serialize culture-sensitive data, such as date and time strings to be passed to the date and time parsing methods, you should use the invariant culture or a specific custom format string.

Dynamic Culture Data

Except for the invariant culture, culture data is dynamic. This is true even for the predefined cultures. For example, countries or regions adopt new currencies, change their spellings of words, or change their preferred calendar, and culture definitions change to track this. Custom cultures are subject to change without notice, and any specific culture might be overridden by a custom replacement culture. Also, as discussed below, an individual user can override cultural preferences. Applications should always obtain culture data at run time.

Caution noteCaution

When saving data, your application should use the invariant culture, use a binary format, or use a specific culture-independent format. Data saved according to the current values associated with a particular culture, other than the invariant culture, might become unreadable or might change in meaning if that culture changes.

CultureInfo Object Serialization

When a CultureInfo object is serialized, all that is actually stored is Name and UseUserOverride. It is successfully de-serialized only in an environment where that Name has the same meaning. The following three examples show why this is not always the case:

  • If CultureTypes indicates CultureTypes.WindowsOnlyCultures, and if that culture was first introduced in Windows Vista, it is not possible to de-serialize it on Windows XP. Similarly, if the culture was first introduced in Windows XP Service Pack 2, it is not possible to de-serialize it for a Windows XP system on which the culture has not been installed.

  • If CultureTypes indicates CultureTypes.UserCustomCulture, and the computer on which it is de-serialized does not have this user custom culture installed, it is not possible to de-serialize it.

  • If CultureTypes indicates CultureTypes.ReplacementCultures, and the computer on which it is de-serialized does not have this replacement culture, it de-serializes to the same name, but not all of the same characteristics. For example, if "en-US" is a replacement culture on computer A, but not on computer B, and if a CultureInfo object referring to this culture is serialized on computer A and de-serialized on computer B, then none of the custom characteristics of the culture are transmitted. The culture de-serializes successfully, but with a different meaning.

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. For more information, see Cultures Generated from Windows Locales.

Control Panel Overrides

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of 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. In general, your applications should honor these user overrides.

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, .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 the regional and language options portion of Control Panel, the currency might be incorrect. To use the .NET Framework default setting for the currency, the application should use a CultureInfo constructor overload that accepts a useUserOverride parameter and set it to false.

Alternate Sort Orders

The Spanish (Spain) culture uses two culture identifiers, 0x0C0A using the default international sort order, and 0x040A using the traditional sort order. If the CultureInfo is constructed using the es-ES culture name, the new CultureInfo uses the default international sort order. For the traditional sort order, the object is constructed using the name es-ES_tradnl. For information on other cultures that have alternate sorts, see Comparing and Sorting Data for a Specific Culture.

Implemented Interfaces

This class implements the ICloneable interface to enable duplication of CultureInfo objects. It also implements IFormatProvider to supply formatting information to applications.

Cultures, Threads, and Application Domains

There are unique considerations when using a thread associated with a CultureInfo object. For more information about cultures and application domains, see Application Domains and Threads.

The following code example shows how to create a CultureInfo object for Spanish (Spain) with the international sort and another CultureInfo object with the traditional sort.


using System;
using System.Collections;
using System.Globalization;

public class SamplesCultureInfo
{

   public static void Main()
   {

      // Creates and initializes the CultureInfo which uses the international sort.
      CultureInfo myCIintl = new CultureInfo("es-ES", false);

      // Creates and initializes the CultureInfo which uses the traditional sort.
      CultureInfo myCItrad = new CultureInfo(0x040A, false);

      // Displays the properties of each culture.
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL");
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "CompareInfo", myCIintl.CompareInfo, myCItrad.CompareInfo);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "DisplayName", myCIintl.DisplayName, myCItrad.DisplayName);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "EnglishName", myCIintl.EnglishName, myCItrad.EnglishName);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "IsNeutralCulture", myCIintl.IsNeutralCulture, myCItrad.IsNeutralCulture);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "IsReadOnly", myCIintl.IsReadOnly, myCItrad.IsReadOnly);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "LCID", myCIintl.LCID, myCItrad.LCID);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "Name", myCIintl.Name, myCItrad.Name);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "NativeName", myCIintl.NativeName, myCItrad.NativeName);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "Parent", myCIintl.Parent, myCItrad.Parent);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "TextInfo", myCIintl.TextInfo, myCItrad.TextInfo);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "ThreeLetterISOLanguageName", myCIintl.ThreeLetterISOLanguageName, myCItrad.ThreeLetterISOLanguageName);
      Console.WriteLine("{0,-31}{1,-47}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl.ThreeLetterWindowsLanguageName, myCItrad.ThreeLetterWindowsLanguageName);
      Console.WriteLine("{0,-31}{1,-47}{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"));

   }

}

/*
This code produces the following output.

PROPERTY                       INTERNATIONAL                                  TRADITIONAL              
CompareInfo                    CompareInfo - es-ES                            CompareInfo - es-ES_tradnl
DisplayName                    Spanish (Spain)                                Spanish (Spain)          
EnglishName                    Spanish (Spain, International Sort)            Spanish (Spain, Traditional Sort)
IsNeutralCulture               False                                          False                    
IsReadOnly                     False                                          False                    
LCID                           3082                                           1034                     
Name                           es-ES                                          es-ES                    
NativeName                     Español (España, alfabetización internacional) Español (España, alfabetización tradicional)
Parent                         es                                             es                       
TextInfo                       TextInfo - es-ES                               TextInfo - es-ES_tradnl  
ThreeLetterISOLanguageName     spa                                            spa                      
ThreeLetterWindowsLanguageName ESN                                            ESP                      
TwoLetterISOLanguageName       es                                             es                       

Comparing "llegar" and "lugar"
   With myCIintl.CompareInfo.Compare: -1
   With myCItrad.CompareInfo.Compare: 1

*/


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

NoteNote

The example displays the zh-CHS and zh-CHT culture names with the 0x0004 and 0x7C04 culture identifiers, respectively. However, your Windows Vista applications should use the zh-Hans name instead of zh-CHS and the zh-Hant name instead of zh-CHT. The zh-Hans and zh-Hant names represent the current standard, and should be used unless you have a reason for using the older names.


using System;
using System.Globalization;

public class SamplesCultureInfo
{

   public static void Main()
   {

      // Prints the header.
      Console.WriteLine("SPECIFIC CULTURE                                     PARENT CULTURE");

      // Determines the specific cultures that use the Chinese language, and displays the parent culture.
      foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
      {
         if (ci.TwoLetterISOLanguageName == "zh")
         {
            Console.Write("0x{0} {1} {2,-40}", 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);
         }
      }

   }

}

/*
This code produces the following output.

SPECIFIC CULTURE                                     PARENT CULTURE
0x0404 zh-TW Chinese (Traditional, Taiwan)           0x7C04 zh-CHT Chinese (Traditional) Legacy
0x0804 zh-CN Chinese (Simplified, PRC)               0x0004 zh-CHS Chinese (Simplified) Legacy
0x0C04 zh-HK Chinese (Traditional, Hong Kong S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
0x1004 zh-SG Chinese (Simplified, Singapore)         0x0004 zh-CHS Chinese (Simplified) Legacy
0x1404 zh-MO Chinese (Traditional, Macao S.A.R.)     0x7C04 zh-CHT Chinese (Traditional) Legacy

*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.