7 out of 28 rated this helpful - Rate this topic

CultureInfo Class

Updated: December 2011

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.

  Name Description
Public method Supported by the XNA Framework CultureInfo(Int32) Initializes a new instance of the CultureInfo class based on the culture specified by the culture identifier.
Public method Supported by the XNA Framework Supported by Portable Class Library CultureInfo(String) Initializes a new instance of the CultureInfo class based on the culture specified by name.
Public method Supported by the XNA Framework CultureInfo(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 method Supported by the XNA Framework CultureInfo(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
  Name Description
Public property Supported by the XNA Framework Supported by Portable Class Library Calendar Gets the default calendar used by the culture.
Public property Supported by the XNA Framework Supported by Portable Class Library CompareInfo Gets the CompareInfo that defines how to compare strings for the culture.
Public property CultureTypes Gets the culture types that pertain to the current CultureInfo object.
Public property Static member Supported by the XNA Framework Supported by Portable Class Library CurrentCulture Gets the CultureInfo that represents the culture used by the current thread.
Public property Static member Supported by the XNA Framework Supported by Portable Class Library CurrentUICulture Gets the CultureInfo that represents the current culture used by the Resource Manager to look up culture-specific resources at run time.
Public property Supported by the XNA Framework Supported by Portable Class Library DateTimeFormat Gets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
Public property DisplayName Gets the culture name in the format "<languagefull> (<country/regionfull>)" in the language of the localized version of .NET Framework.
Public property Supported by the XNA Framework Supported by Portable Class Library EnglishName Gets the culture name in the format "<languagefull> (<country/regionfull>)" in English.
Public property IetfLanguageTag Deprecated. Gets the RFC 4646 standard identification for a language.
Public property Static member InstalledUICulture Gets the CultureInfo that represents the culture installed with the operating system.
Public property Static member Supported by the XNA Framework Supported by Portable Class Library InvariantCulture Gets the CultureInfo that is culture-independent (invariant).
Public property Supported by the XNA Framework Supported by Portable Class Library IsNeutralCulture Gets a value indicating whether the current CultureInfo represents a neutral culture.
Public property Supported by the XNA Framework Supported by Portable Class Library IsReadOnly Gets a value indicating whether the current CultureInfo is read-only.
Public property KeyboardLayoutId Gets the active input locale identifier.
Public property Supported by the XNA Framework LCID Gets the culture identifier for the current CultureInfo.
Public property Supported by the XNA Framework Supported by Portable Class Library Name Gets the culture name in the format "languagecode2-country/regioncode2".
Public property Supported by the XNA Framework Supported by Portable Class Library NativeName Gets the culture name, consisting of the language, the country/region, and the optional script, that the culture is set to display.
Public property Supported by the XNA Framework Supported by Portable Class Library NumberFormat Gets or sets a NumberFormatInfo that defines the culturally appropriate format of displaying numbers, currency, and percentage.
Public property Supported by the XNA Framework Supported by Portable Class Library OptionalCalendars Gets the list of calendars that can be used by the culture.
Public property Supported by the XNA Framework Supported by Portable Class Library Parent Gets the CultureInfo that represents the parent culture of the current CultureInfo.
Public property Supported by the XNA Framework Supported by Portable Class Library TextInfo Gets the TextInfo that defines the writing system associated with the culture.
Public property Supported by the XNA Framework ThreeLetterISOLanguageName Gets the ISO 639-2 three-letter code for the language of the current CultureInfo.
Public property Supported by the XNA Framework ThreeLetterWindowsLanguageName Gets the three-letter code for the language as defined in the Windows API.
Public property Supported by the XNA Framework Supported by Portable Class Library TwoLetterISOLanguageName Gets the ISO 639-1 two-letter code for the language of the current CultureInfo.
Public property Supported by the XNA Framework UseUserOverride Gets a value indicating whether the current CultureInfo uses the user-selected culture settings.
Top
  Name Description
Public method Supported by the XNA Framework ClearCachedData Refreshes cached culture-related information.
Public method Supported by the XNA Framework Clone Creates a copy of the current CultureInfo.
Public method Static member Supported by the XNA Framework CreateSpecificCulture Creates a CultureInfo that represents the specific culture that is associated with the specified name.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals Determines whether the specified object is the same culture as the current CultureInfo. (Overrides Object.Equals(Object).)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetConsoleFallbackUICulture Gets an alternate user interface culture suitable for console applications when the default graphic user interface culture is unsuitable.
Public method Static member Supported by the XNA Framework GetCultureInfo(Int32) Retrieves a cached, read-only instance of a culture by using the specified culture identifier.
Public method Static member Supported by the XNA Framework GetCultureInfo(String) Retrieves a cached, read-only instance of a culture by using the specified culture name.
Public method Static member GetCultureInfo(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 method Static member GetCultureInfoByIetfLanguageTag Deprecated. Retrieves a read-only CultureInfo object having linguistic characteristics that are identified by the specified RFC 4646 language tag.
Public method Static member GetCultures Gets the list of supported cultures filtered by the specified CultureTypes parameter.
Public method Supported by the XNA Framework Supported by Portable Class Library GetFormat Gets an object that defines how to format the specified type.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for the current CultureInfo, suitable for hashing algorithms and data structures, such as a hash table. (Overrides Object.GetHashCode().)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ReadOnly Returns a read-only wrapper around the specified CultureInfo.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns 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.

Note Note

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.

Note Note

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

When preparing software to handle custom cultures, consider the following:

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

  • Respect the user's culture data values; 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, date formats, spellings, etc., will probably change in the future. If your application needs to serialize using this data, as for the DateTime formatting and parsing functions, it should use the invariant culture or a specific format.

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 note Caution

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.

Note Note

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.

Date

History

Reason

December 2011

Noted that the case of a culture name is not significant.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
First CultureInfo Sample - recoded in PowerShell
<#
.SYNOPSIS
This script creates a Spanish cultureinfo object with a tradidional
sort and another with an inernational sort. The script then compares them.
.DESCRIPTION
This script re-implements an MSDN sample.
.NOTES
File Name : New-SpanishCulture.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted to:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
.EXAMPLE
C:\foo> .\Show-ChineeseParentCulture.ps1
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
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
#>

# Create and initialize the CultureInfo which uses the international sort
$myCIintl = New-Object System.Globalization.CultureInfo "es-ES", $false

# Create and initialize the CultureInfo which uses the traditional sort
$myCItrad = New-Object System.Globalization.CultureINfo 0x040A, $false

# Display the properties of each culture.
"{0,-31}{1,-47}{2,-25}" -f "PROPERTY", "INTERNATIONAL", "TRADITIONAL"
"{0,-31}{1,-47}{2,-25}" -f "CompareInfo", $myCIintl.CompareInfo, $myCItrad.CompareInfo
"{0,-31}{1,-47}{2,-25}" -f "DisplayName", $myCIintl.DisplayName, $myCItrad.DisplayName
"{0,-31}{1,-47}{2,-25}" -f "EnglishName", $myCIintl.EnglishName, $myCItrad.EnglishName
"{0,-31}{1,-47}{2,-25}" -f "IsNeutralCulture", $myCIintl.IsNeutralCulture, $myCItrad.IsNeutralCulture
"{0,-31}{1,-47}{2,-25}" -f "IsReadOnly", $myCIintl.$IsReadOnly, $myCItrad.IsReadOnly
"{0,-31}{1,-47}{2,-25}" -f "LCID", $myCIintl.LCID, $myCItrad.LCID
"{0,-31}{1,-47}{2,-25}" -f "Name", $myCIintl.Name, $myCItrad.Name
"{0,-31}{1,-47}{2,-25}" -f "NativeName", $myCIintl.NativeName, $myCItrad.NativeName
"{0,-31}{1,-47}{2,-25}" -f "Parent", $myCIintl.Parent, $myCItrad.Parent
"{0,-31}{1,-47}{2,-25}" -f "TextInfo", $myCIintl.TextInfo, $myCItrad.TextInfo
"{0,-31}{1,-47}{2,-25}" -f "ThreeLetterISOLanguageName", $myCIintl.ThreeLetterISOLanguageName, $myCItrad.ThreeLetterISOLanguageName
"{0,-31}{1,-47}{2,-25}" -f "ThreeLetterWindowsLanguageName",$myCIintl.ThreeLetterWindowsLanguageName, $myCItrad.ThreeLetterWindowsLanguageName
"{0,-31}{1,-47}{2,-25}" -f "TwoLetterISOLanguageName", $myCIintl.TwoLetterISOLanguageName, $myCItrad.TwoLetterISOLanguageName
""

# Compare two strings using myCIintl
"Comparing [llegar] and [lugar]"
" With myCIintl.CompareInfo.Compare: {0}" -f $myCIintl.CompareInfo.Compare("llegar", "lugar")
" With myCItrad.CompareInfo.Compare: {0}" -f $myCItrad.CompareInfo.Compare("llegar", "lugar")
Second Sample - Recoded in PowerShell
<#
.SYNOPSIS
This script displays the parent culture of each
specific culture using the Chinese language.
.DESCRIPTION
This script looks at each Chineese culture and displays
the culture name and the parent.
.NOTES
File Name : Show-ChineeseParentCulture.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted to:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
.EXAMPLE
C:\foo> .\Show-ChineeseParentCulture.ps1
SPECIFIC CULTURE PARENT CULTURE
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
0x1404 zh-MO Chinese (Traditional, Macao S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
0x1004 zh-SG Chinese (Simplified, Singapore) 0x0004 zh-CHS Chinese (Simplified) Legacy
0x0404 zh-TW Chinese (Traditional, Taiwan) 0x7C04 zh-CHT Chinese (Traditional) Legacy
#>

# Display a header
"SPECIFIC CULTURE PARENT CULTURE"

# Determine the specific cultures that use the Chinese language, and displays the parent culture

ForEach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures)) {
if ($ci.TwoLetterISOLanguageName -eq "zh")
{
$s1 = "0x{0} {1} {2,-40}" -f $ci.LCID.ToString("X4"), $ci.Name, $ci.EnglishName
$s2 = "0x{0} {1} {2}" -f $ci.Parent.LCID.ToString("X4"), $ci.Parent.Name, $ci.Parent.EnglishName
"{0}{1}" -f $s1, $s2
}
}
Casing of constructor strings

It seems (not tested to any extent, apologies if incorrect) that you can pass lowercase strings to the constructor without any problems.
"en-gb" gives same result as "en-GB"
"en-us" gives same result as "en-US"
Just handy to know particularly if you are working on the web and want lowercase urls. Hope that helps someone.

Culture Names Are Case-Insensitive

Thanks for pointing that out, MemeDeveloper. When passed to class constructors or CultureInfo methods such as GetCultureInfo or CreateSpecificCulture, culture names are not case-sensitive; they do not have to match the case of the culture name. We're revising the documentation to note this in a number of appropriate places.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

How can I set the CultureInfo for a single run of a program

Suppose I have a .Net program prog.exe which will use the default culture. Can I set the culture to (say) 'fr' for one invocation of the program? $0Perhaps at the command line, something like:$0 $0$0 $0 $0&gt; set CULTURE=fr$0 $0&gt; .\prog.exe$0 $0$0 $0 $0Is something like this possible?$0

Setting the Culture for a Single Program Invocation

I'm not sure what your precise scenario is -- whether the issue is that you have an application that was inadvertently written to use the default culture, you can't modify the source code and recompile, and the application really was intended to use the conventions of a particular culture rather than the default culture, or whether you want to test an application that uses the conventions of the default culture against a number of different cultures. In either scenario, there are a number of ways to do this (I'll focus on the third of these). All of them involve developing a wrapper that will execute the main application. The three ways (and there may be more) are:

  • Create an application domain, and call an overload of the CreateInstance, CreateInstanceAndUnwrap, CreateInstanceFrom, or CreateInstanceFromAndUnwrap methods that includes a parameter of type CultureInfo that defines the culture you'd like to make the application's current culture. The methods create an instance of your application; you can then execute its startup method.
  • Derive a class from AppDomainManager and set its default culture in your override to the InitializeNewDomain method.
  • Create a wrapper application that sets the culture of the current thread, loads the assembly containing your application, and then dynamically invokes its entry point.

Taking the last approach, if your application is a console application in an assembly whose path and filename is C:\Test\MyApp.exe, the following code loads that assembly using a specified culture (in this case, French (France)):

using System;
using System.Globalization;
using System.Reflection;
using System.Threading;






public class Example
{
const string FILEPATH = @"C:\Test\MyApp.exe";

public static void Main()
{
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine("Current culture is {0}", CultureInfo.CurrentCulture.Name);

Assembly assem = Assembly.LoadFile(FILEPATH);
Type exeType = assem.GetType("Example");
MethodInfo meth = exeType.GetMethod("Main");
meth.Invoke(null, null);
}
}


In this code, the culture name is hard-coded, although it could just as easily be a supplied as a command-line parameter or read from a configuration file.

There is a major limitation to all of these approaches. Since all three provide wrappers around what is really an unknown executable, their attempt to set the culture for the application will fail under the following conditions:

  1. The application calls overloads that explicitly specify the culture to be used.
  2. The application has its own app domain manager or executes in a separate application domain.
  3. The application is multi-threaded. While this will set the culture of the thread on which the application is launched, any additional threads that the application creates will use the system culture.

--Ron Petrusha
Developer Division User Education
Microsoft Corporation




CultureInfo Names
http://authors.aspalliance.com/aspxtreme/sys/globalization/demos/CultureInfo.aspx