CultureAndRegionInfoBuilder 类

定义

定义一个新型或基于其他区域性和国家/地区的自定义区域性。Defines a custom culture that is new or based on another culture and country/region. 自定义区域性可以安装在计算机上,然后由运行在该计算机上的任何应用程序使用。The custom culture can be installed on a computer and subsequently used by any application that is running on that computer. 此类不能被继承。This class cannot be inherited.

public ref class CultureAndRegionInfoBuilder sealed
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class CultureAndRegionInfoBuilder
type CultureAndRegionInfoBuilder = class
Public NotInheritable Class CultureAndRegionInfoBuilder
继承
CultureAndRegionInfoBuilder
属性

示例

下面的示例定义了一个自定义 ru 区域性, 它表示美国中的俄文语言。The following example defines a custom ru-US culture that represents the Russian language in the United States. 该示例通过从俄语 (俄罗斯) CultureInfo对象和美国RegionInfo对象加载设置来定义自定义区域性, 然后设置多个CultureAndRegionInfoBuilder属性。The example defines the custom culture by loading settings from the Russian (Russia) CultureInfo object and the U.S. RegionInfo object, and then sets a number of CultureAndRegionInfoBuilder properties. 该示例将注册自定义区域性, 然后将其实例化, 并使其成为当前线程的区域性。The example registers the custom culture, and then instantiates it and makes it the current thread culture.

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

public class Example
{
   public static void Main()
   {
      // Create a custom culture for ru-US.
      CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", 
                                             CultureAndRegionModifiers.None);
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
      car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));
      
      car1.CultureEnglishName = "Russian (United States)";
      car1.CultureNativeName = "русский (США)";
      car1.CurrencyNativeName = "Доллар (США)";
      car1.RegionNativeName = "США";

      // Register the culture.
      try {
         car1.Register();
      }    
      catch (InvalidOperationException) {
         // Swallow the exception: the culture already is registered.
      }
      
      // Use the custom culture.
      CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US");
      Thread.CurrentThread.CurrentCulture = ci;
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo);
   }
}
// The example displays the following output:
//     Current Culture: ru-US
//     Writing System: TextInfo - ru-US
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Create a custom culture for ru-US.
      Dim car1 As New CultureAndRegionInfoBuilder("ru-US", CultureAndRegionModifiers.None)
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"))
      car1.LoadDataFromRegionInfo(New RegionInfo("en-US"))
      
      car1.CultureEnglishName = "Russian (United States)"
      car1.CultureNativeName = "русский (США)"
      car1.CurrencyNativeName = "Доллар (США)"
      car1.RegionNativeName = "США"

      ' Register the culture.
      Try
         car1.Register()
      Catch e As InvalidOperationException
         ' Swallow the exception: the culture already is registered.
      End Try
      
      ' Use the custom culture.
      Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("ru-US")
      Thread.CurrentThread.CurrentCulture = ci
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo)
   End Sub
End Module
' The example displays the following output:
'     Current Culture: ru-US
'     Writing System: TextInfo - ru-US

注解

CultureInfo类保存特定于区域性的信息, 如关联的语言、子语言、国家/地区、日历和文化约定。The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions. 此类还提供DateTimeFormatInfo了、 NumberFormatInfoCompareInfoTextInfo类的区域性特定实例, 这些实例是特定于区域性的操作 (如大小写、格式化和分析日期和数字) 所必需的, 并且比较字符串。This class also provides culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo classes, which are required for culture-specific operations such as casing, formatting and parsing dates and numbers, and comparing strings.

默认情况下, .NET Framework 支持CultureInfo表示一组预定义的区域性的对象。By default, the .NET Framework supports CultureInfo objects that represent a predefined set of cultures. 有关 Windows 系统上提供的这些区域性的列表, 请参阅windows 支持的语言/区域名称列表中的语言标记列。For a list of these cultures available on Windows systems, see the Language tag column in the list of language/region names supported by Windows. 列名遵循 BCP 47 定义的标准。Culture names follow the standard defined by BCP 47. 利用CultureAndRegionInfoBuilder类, 您可以创建一个完全新的或重写预定义区域性的自定义区域性。The CultureAndRegionInfoBuilder class enables you to create a custom culture that is completely new or that overrides a predefined culture. 当在特定计算机上安装和注册自定义区域性时, 它将无法与预CultureInfo定义的对象进行区分, 并且可以实例化和使用, 就像这些对象一样。When a custom culture is installed and registered on a particular computer, it becomes indistinguishable from predefined CultureInfo objects, and can be instantiated and used just like those objects.

重要

请注意, CultureAndRegionInfoBuilder在名为 sysglobl 的程序集中找到了该类。Note that the CultureAndRegionInfoBuilder class is found in an assembly named sysglobl.dll. 如果成功编译使用此类型的代码, 则需要添加对 sysglobl 的引用。Successfully compiling code that uses this type requires that you add a reference to sysglobl.dll.

自定义区域性只能由对该计算机拥有管理权限的用户在计算机上进行注册。A custom culture can be registered on a computer only by a user who has administrative rights on that computer. 因此, 应用通常不会创建和安装自定义区域性。Consequently, apps typically do not create and install custom cultures. 相反, 您可以使用CultureAndRegionInfoBuilder类创建一个特殊用途的工具, 管理员可以使用该工具创建、安装和注册自定义区域性。Instead, you can use the CultureAndRegionInfoBuilder class to create a special-purpose tool that an administrator can use to create, install, and register a custom culture. 在计算机上注册自定义区域性后, 可以在应用程序中CultureInfo使用类来创建自定义区域性的实例, 就像针对预定义的区域性所做的一样。After the custom culture is registered on a computer, you can use the CultureInfo class in your app to create instances of the custom culture just as you would for a predefined culture.

如果分析为自定义区域性生成的日期和时间字符串, 则应DateTime.ParseExact使用或DateTime.TryParseExact方法, 而不是DateTime.ParseDateTime.TryParse方法来提高分析操作成功的概率。If you parse date and time strings generated for a custom culture, you should use the DateTime.ParseExact or DateTime.TryParseExact method instead of the DateTime.Parse or DateTime.TryParse method to improve the probability that the parse operation will succeed. 自定义区域性的日期和时间字符串可能比较复杂, 因此难以分析。A date and time string for a custom culture can be complicated and therefore difficult to parse. ParseTryParse方法尝试使用几个隐式分析模式分析字符串, 所有这些模式都可能失败。The Parse and TryParse methods try to parse a string with several implicit parse patterns, all of which might fail. TryParseExact此相反, 方法要求应用程序显式指定一个或多个可能成功的完全分析模式。The TryParseExact method, in contrast, requires the application to explicitly designate one or more exact parse patterns that are likely to succeed.

定义和创建自定义区域性Defining and Creating a Custom Culture

使用CultureAndRegionInfoBuilder类定义和命名自定义区域性。You use the CultureAndRegionInfoBuilder class to define and name a custom culture. 自定义区域性可以是全新的区域性、基于现有区域性的新区域性 (即补充区域性) 或替换现有 .NET Framework 区域性的区域性。The custom culture can be an entirely new culture, a new culture that is based on an existing culture (that is, a supplemental culture), or a culture that replaces an existing .NET Framework culture. 在每种情况下, 基本步骤都是相同的:In each case, the basic steps are the same:

  1. CultureAndRegionInfoBuilder通过调用对象的构造函数CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers)来实例化对象。Instantiate a CultureAndRegionInfoBuilder object by calling its CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers) constructor. 若要替换现有的区域性, 请将该区域性的名称CultureAndRegionModifiers.Replacement和枚举值传递到构造函数。To replace an existing culture, pass that culture's name and the CultureAndRegionModifiers.Replacement enumeration value to the constructor. 若要创建新的区域性或补充区域性, 请传递唯一的CultureAndRegionModifiers.Neutral区域性名称和或CultureAndRegionModifiers.None枚举值。To create a new culture or a supplemental culture, pass a unique culture name and either the CultureAndRegionModifiers.Neutral or CultureAndRegionModifiers.None enumeration value.

    备注

    如果使用CultureAndRegionModifiers.Replacement枚举值CultureAndRegionInfoBuilder实例化对象, 则该CultureAndRegionInfoBuilder对象的属性将自动用要替换的CultureInfo对象中的值进行填充。If you use the CultureAndRegionModifiers.Replacement enumeration value to instantiate a CultureAndRegionInfoBuilder object, the CultureAndRegionInfoBuilder object's properties are automatically populated with values from the CultureInfo object to be replaced.

  2. 如果要创建新的或补充的区域性:If you are creating a new or supplemental culture:

  3. 根据需要修改CultureAndRegionInfoBuilder对象的属性。Modify the properties of the CultureAndRegionInfoBuilder object as necessary.

  4. 如果打算在单独的例程中注册自定义区域性, 请调用Save方法。If you are planning to register the custom culture in a separate routine, call the Save method. 这会生成一个 XML 文件, 你可以在单独的自定义区域性安装例程中加载和注册该文件。This generates an XML file that you can load and register in a separate custom culture installation routine.

注册自定义区域性Registering a Custom Culture

如果正在开发的自定义区域性的注册应用程序与创建区域性的应用程序不同, 则调用CreateFromLdml方法以加载包含自定义区域性定义的 XML 文件, 并实例化CultureAndRegionInfoBuilder对象。If you are developing a registration application for a custom culture that is separate from the application that creates the culture, you call the CreateFromLdml method to load the XML file that contains the custom culture's definition and instantiate the CultureAndRegionInfoBuilder object. 若要处理注册, 请调用Register方法。To handle the registration, call the Register method. 若要成功注册, 注册自定义区域性的应用程序必须在目标系统上以管理权限运行;否则, 对的调用RegisterUnauthorizedAccessException引发异常。For the registration to succeed, the application that registers the custom culture must be running with administrative privileges on the target system; otherwise, the call to Register throws an UnauthorizedAccessException exception.

警告

系统之间的区域性数据可能不同。Culture data can differ between systems. 如果使用CultureAndRegionInfoBuilder类创建在多个系统之间统一的自定义区域性, 并通过从现有CultureInfoRegionInfo对象加载数据并对其进行自定义来创建自定义区域性, 则应开发两个不同的实用程序。If you are using the CultureAndRegionInfoBuilder class to create a custom culture that is uniform across multiple systems and you are creating your custom culture by loading data from existing CultureInfo and RegionInfo objects and customizing it, you should develop two different utilities. 首先创建自定义区域性, 并将其保存到 XML 文件中。The first creates the custom culture and saves it to an XML file. 第二种CreateFromLdml方法使用方法从 XML 文件加载自定义区域性, 并将其注册到目标计算机上。The second uses the CreateFromLdml method to load the custom culture from an XML file and register it on the target computer.

注册过程将执行以下任务:The registration process performs the following tasks:

  • 创建一个 nlp 文件, 其中包含在CultureAndRegionInfoBuilder对象中定义的信息。Creates an .nlp file that contains the information that is defined in the CultureAndRegionInfoBuilder object.

  • 将 nlp 文件存储在目标计算机上的%windir%\Globalization 系统目录中。Stores the .nlp file in the %windir%\Globalization system directory on the target computer. 这使自定义区域性的设置可以在会话之间保持。This enables the custom culture's settings to persist between sessions. (该CultureAndRegionInfoBuilder方法需要管理权限, 因为 nlp 文件存储在系统目录中。)(The CultureAndRegionInfoBuilder method requires administrative privileges because the .nlp file is stored in a system directory.)

  • 准备 .NET Framework 以便在下次请求创建新的自定义区域性时搜索%windir%\Globalization 系统目录而不是内部缓存。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.

成功注册自定义区域性后, 它无法与 .NET Framework 预定义的区域性区分开来。When a custom culture is successfully registered, it is indistinguishable from the cultures that are predefined by the .NET Framework. 自定义区域性可用到CultureAndRegionInfoBuilder方法调用从本地计算机中删除 nlp 文件。The custom culture is available until a call to the CultureAndRegionInfoBuilder method removes the .nlp file from the local computer.

实例化自定义区域性Instantiating a Custom Culture

可以通过以下方式之一创建自定义区域性的实例:You can create an instance of the custom culture in one of the following ways:

此外, CultureInfo CultureInfo.GetCultures由方法返回的对象数组包含自定义区域性。In addition, the array of CultureInfo objects that is returned by the CultureInfo.GetCultures method includes the custom culture.

构造函数

CultureAndRegionInfoBuilder(String, CultureAndRegionModifiers)

初始化 CultureAndRegionInfoBuilder 类的新实例。Initializes a new instance of the CultureAndRegionInfoBuilder class.

属性

AvailableCalendars

获取或设置此 CultureAndRegionInfoBuilder 对象支持的日历数组。Gets or sets an array of calendars that are supported by this CultureAndRegionInfoBuilder object.

CompareInfo

获取或设置用来定义如何根据区域性比较字符串的 CompareInfo 对象。Gets or sets the CompareInfo object that defines how to compare strings for the culture.

ConsoleFallbackUICulture

当默认图形用户界面区域性不合适时,获取或设置一个适合控制台应用程序的替换用户界面区域性。Gets or sets an alternate user interface culture suitable for console applications when the default graphic user interface culture is inappropriate.

CultureEnglishName

获取或设置英文的区域性名称。Gets or sets the culture name in English.

CultureName

获取所创建区域性的名称。Gets the name of the culture being created.

CultureNativeName

按照区域性设置显示的格式和语言获取或设置区域性名称。Gets or sets the culture name in the format and language that the culture is set to display.

CultureTypes

获取 CultureTypes 值,该值描述了由当前 CultureAndRegionInfoBuilder 对象表示的区域性。Gets the CultureTypes value that describes the culture represented by the current CultureAndRegionInfoBuilder object.

CurrencyEnglishName

获取或设置由当前 CultureAndRegionInfoBuilder 对象表示的国家/地区中所用货币的英文名称。Gets or sets the name, in English, of the currency used in the country/region represented by the current CultureAndRegionInfoBuilder object.

CurrencyNativeName

获取或设置由当前 CultureAndRegionInfoBuilder 对象表示的国家/地区中所用货币的本地名称。Gets or sets the native name of the currency used in the country/region represented by the current CultureAndRegionInfoBuilder object.

GeoId

获取或设置地理区域、国家/地区、城市或位置的唯一标识号。Gets or sets a unique identification number for a geographical region, country, city, or location.

GregorianDateTimeFormat

获取或设置根据公历定义日期和时间格式的 DateTimeFormatInfo 对象。Gets or sets a DateTimeFormatInfo object that defines the format of dates and times according to the Gregorian calendar.

IetfLanguageTag

获取或设置根据 RFC 4646 标准“Tags for the Identification of Languages”(语言标识标记)进行格式化的区域性名称。Gets or sets a culture name formatted according to the RFC 4646 standard, "Tags for the Identification of Languages."

IsMetric

获取或设置一个值,该值指示国家/地区是否使用公制度量单位。Gets or sets a value indicating whether the country/region uses the metric system for measurements.

ISOCurrencySymbol

获取或设置与国家/地区关联的由三个字符组成的 ISO 4217 货币符号。Gets or sets the three-character ISO 4217 currency symbol associated with the country/region.

IsRightToLeft

获取或设置与当前 CultureAndRegionInfoBuilder 对象关联的书写体系中文本行的主导方向。Gets or sets the predominant direction of lines of text in the writing system associated with the current CultureAndRegionInfoBuilder object.

KeyboardLayoutId

获取或设置活动的输入区域设置标识符。Gets or sets the active input locale identifier.

LCID

获取当前 CultureAndRegionInfoBuilder 对象的区域性标识符。Gets the culture identifier for the current CultureAndRegionInfoBuilder object.

NumberFormat

获取或设置一个 NumberFormatInfo 对象,它定义相应于区域性的数字、货币和百分比显示格式。Gets or sets a NumberFormatInfo object that defines the culturally appropriate format of displaying numbers, currency, and percentage.

Parent

获取或设置表示当前自定义区域性的父区域性的 CultureInfo 对象。Gets or sets the CultureInfo object that represents the parent culture of the current custom culture.

RegionEnglishName

获取或设置以英文表示的国家/地区的全名。Gets or sets the full name of the country/region in English.

RegionName

获取当前 CultureAndRegionInfoBuilder 对象的国家/地区的名称。Gets the name of the country/region for the current CultureAndRegionInfoBuilder object.

RegionNativeName

获取或设置此自定义区域性的人们所知道的国家/地区的全名。Gets or sets the full name of the country/region as known by the people of this custom culture.

TextInfo

获取或设置定义与此自定义区域性关联的书写体系的 TextInfo 对象。Gets or sets the TextInfo object that defines the writing system associated with this custom culture.

ThreeLetterISOLanguageName

获取或设置此自定义区域性的语言的由三个字母组成的 ISO 639-2 代码。Gets or sets the ISO 639-2 three-letter code for the language of this custom culture.

ThreeLetterISORegionName

获取或设置在 ISO 3166 中定义的由三个字母组成的国家/地区代码。Gets or sets the three-letter code defined in ISO 3166 for the country/region.

ThreeLetterWindowsLanguageName

获取或设置 Windows API 中定义的由三个字母构成的语言代码。Gets or sets the three-letter code for the language as defined in the Windows API.

ThreeLetterWindowsRegionName

获取或设置 Windows 为当前自定义区域性表示的国家/地区分配的由三个字母组成的代码。Gets or sets the three-letter code assigned by Windows to the country/region represented by the current custom culture.

TwoLetterISOLanguageName

获取或设置当前 CultureInfo 对象的语言的由两个字母组成的 ISO 639-1 代码。Gets or sets the ISO 639-1 two-letter code for the language of the current CultureInfo object.

TwoLetterISORegionName

获取或设置在 ISO 3166 中定义的由两个字母组成的国家/地区代码。Gets or sets the two-letter code defined in ISO 3166 for the country/region.

方法

CreateFromLdml(String)

用一个包含对象表示形式的指定 XML 文件重构 CultureAndRegionInfoBuilder 对象。Reconstitutes a CultureAndRegionInfoBuilder object from a specified XML file that contains a representation of the object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
LoadDataFromCultureInfo(CultureInfo)

用指定 CultureInfo 对象的相应属性设置当前 CultureAndRegionInfoBuilder 对象的属性。Sets the properties of the current CultureAndRegionInfoBuilder object with the corresponding properties of the specified CultureInfo object.

LoadDataFromRegionInfo(RegionInfo)

用指定 RegionInfo 对象的相应属性设置当前 CultureAndRegionInfoBuilder 对象的属性。Sets the properties of the current CultureAndRegionInfoBuilder object with the corresponding properties of the specified RegionInfo object.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Register()

将当前 CultureAndRegionInfoBuilder 对象保持为本地计算机上的一个自定义区域性,并使该区域性可供应用程序使用。Persists the current CultureAndRegionInfoBuilder object as a custom culture on the local computer and makes that culture available to applications. 需要管理特权。Requires administrative privileges.

Save(String)

将当前 CultureAndRegionInfoBuilder 对象的 XML 表示形式写入指定的文件。Writes an XML representation of the current CultureAndRegionInfoBuilder object to the specified file.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
Unregister(String)

从本地计算机中删除自定义区域性。Deletes a custom culture from the local computer.

适用于