CultureInfo 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供有关特定区域性的信息(称为非托管代码开发 的区域设置 )。 这些信息包括区域性的名称、写入系统、使用的日历、字符串的排序顺序以及日期和数字的格式。
public ref class CultureInfo : IFormatProvider
public ref class CultureInfo : ICloneable, IFormatProvider
public class CultureInfo : IFormatProvider
public class CultureInfo : ICloneable, IFormatProvider
[System.Serializable]
public class CultureInfo : ICloneable, IFormatProvider
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CultureInfo : ICloneable, IFormatProvider
type CultureInfo = class
interface IFormatProvider
type CultureInfo = class
interface ICloneable
interface IFormatProvider
[<System.Serializable>]
type CultureInfo = class
interface ICloneable
interface IFormatProvider
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CultureInfo = class
interface ICloneable
interface IFormatProvider
Public Class CultureInfo
Implements IFormatProvider
Public Class CultureInfo
Implements ICloneable, IFormatProvider
- 继承
-
CultureInfo
- 属性
- 实现
注解
该 CultureInfo 类提供与特定区域性关联的信息,例如语言、子语言、国家/地区、日历和约定。 此类还提供对 DateTimeFormatInfo、NumberFormatInfo、CompareInfo 和 TextInfo 对象的文化特定的实例的访问。 这些对象包含文化相关操作所需的信息,例如字符大小写、格式化日期和数字以及进行字符串比较。 CultureInfo 类被那些设置格式、解析或操作区域性特定数据的类(例如 String、DateTime、DateTimeOffset 和数值类型)直接或间接使用。
文化名称和标识符
CultureInfo 类根据 RFC 4646 为每种语言文化指定唯一的名称。 该名称是一个与语言相关的 ISO 639 双小写或三小写字母的区域性代码和一个与国家/地区相关的 ISO 3166 双大写字母子区域性代码的组合。 此外,对于在 Windows 10 或更高版本下运行的应用,支持对应于有效 BCP-47 语言标记的区域性名称。
注释
将区域性名称传递给类构造函数或方法(如 CreateSpecificCulture 或 CultureInfo)时,其大小写并不重要。
基于 RFC 4646 的区域性名称的格式是 languagecode2-country/regioncode2,其中 languagecode2 是双字母语言代码,country/regioncode2 是双字母子区域性代码。 示例包括日语(日本)的 ja-JP 和英语(美国)的 en-US。 在双字母语言代码不可用的情况下,使用 ISO 639-3 中定义的三字母代码。
某些文化名称还指定 ISO 15924 字符集。 例如,Cyrl 指定西里尔语脚本,Latn 指定拉丁语脚本。 包含脚本的区域性名称使用模式 languagecode2-scripttag-country/regioncode2。 此类文化名称的一个示例是乌兹别克语的 uz-Cyrl-UZ(西里尔字母,乌兹别克斯坦)。 在 Windows Vista 之前的 Windows 操作系统上,包含脚本的区域性名称使用模式 languagecode2-country/regioncode2-scripttag,例如,乌兹别克语的 uz-UZ-Cyrl(西里尔语,乌兹别克斯坦)。
中立文化仅通过双字母小写语言代码来指定。 例如,fr 指定法语的非特定区域性,de 指定德语的非特定区域性。
注释
有两个文化名称与此规则相矛盾。 中文(简体),名为 zh-Hans,和中文(繁体),名为 zh-Hant,是中立文化。 除非您有理由使用旧名称 zh-CHS 和 zh-CHT,否则这些文化名称表示当前标准并应使用。
区域性标识符是一个标准的国际数字缩写,包含用于唯一识别已安装区域性之一的必要组件。 应用程序可以使用预定义的区域性标识符,或者定义自定义标识符。
某些预定义区域性名称和标识符由 System.Globalization 命名空间中的该类和其他类使用。 有关 Windows 系统的详细区域性信息,请参阅 Windows 支持的语言/区域名称列表中的“语言标记”列。 文化名称遵循 BCP 47 定义的标准。
文化名称和标识符仅表示可在特定计算机上找到的文化的子集。 Windows 版本或服务包可以更改可用的区域性。 应用程序可以使用 CultureAndRegionInfoBuilder 类添加自定义区域性。 用户可以使用 Microsoft Locale Builder 工具添加自己的自定义文化信息。 Microsoft Locale Builder 是使用 CultureAndRegionInfoBuilder 类以托管代码编写的。
多个独特名称与文化密切相关,尤其是与以下类别成员关联的名称:
不变文化、中立文化和特定文化
这些文化通常分为三组:不变文化、中性文化和特定文化。
不变文化是与文化无关的。 应用程序通过使用空字符串("")或其标识符来指定不变区域性。
InvariantCulture 定义不变文化的实例。 它与英语相关联,但不与任何国家/地区相关联。 命名空间Globalization中几乎任何需要文化设置的方法都会用到它。
中性文化是一种与语言相关但不与国家或地区相关的文化。 特定文化是与语言和国家/地区相关联的一种文化。 例如,fr 是法语文化的通用名称,fr-FR 是法语(法国)文化的特定名称。 请注意,中文(简体)和中文(繁体)也被视为中立的文化。
不建议为中性文化创建 CompareInfo 类的实例,因为其中的数据是没有固定标准的。 若要显示和排序数据,请同时指定语言和区域。 此外,为中性文化创建的 Name 对象的 CompareInfo 属性仅返回国家,而不包括区域。
定义的区域性具有层次结构,其中特定区域性的父区域性是中性区域性,中性区域性的父区域性是不变区域性。 Parent 属性包含与特定文化关联的中性文化。 自定义区域性应定义符合此模式的 Parent 属性。
如果操作系统中没有特定文化的资源,则使用相关的中性文化资源。 如果中性文化的资源不可用,则使用主程序集内嵌入的资源。 有关资源回退进程的详细信息,请参阅打包和部署资源。
Windows API 中的区域设置列表与 .NET 支持的文化列表略有不同。 如果需要与 Windows 实现互操作性,比如通过 p/invoke 机制,应用程序应使用操作系统所定义的特定区域性。 使用特定文化可确保与对应的 Windows 区域设置保持一致,该区域设置由具有与 LCID 相同的区域设置标识符来标识。
只能为不变种或特定区域性创建DateTimeFormatInfo或NumberFormatInfo,而不能为中性区域性创建。
如果 DateTimeFormatInfo.Calendar 是 TaiwanCalendar 但 Thread.CurrentCulture 未设置为 zh-TW,则 DateTimeFormatInfo.NativeCalendarName、DateTimeFormatInfo.GetEraName 和 DateTimeFormatInfo.GetAbbreviatedEraName 返回一个空字符串("")。
自定义文化
在 Windows 上,可以创建自定义区域设置。 有关详细信息,请参阅自定义区域设置。
CultureInfo 和文化数据
.NET从两个不同的源派生其文化数据,具体取决于 OS:
- 在 Unix 平台或Windows 10及更高版本上, Unicode(ICU) 库的国际组件提供文化数据。 ICU 库的特定版本取决于操作系统。
- 在Windows 8.1和早期版本中,Windows操作系统提供文化数据。
因此,在特定的 .NET 实现、平台或版本上可用的区域性可能在另一个 .NET 实现、平台或版本上不可用。
某些 CultureInfo 对象因基础平台而有所不同。 具体而言,zh-CN(中文(简体,中国))和 zh-TW(中文(繁体,台湾))在 Windows 系统上是可用的文化设置,但在 Unix 系统上它们是别名文化设置。 “zh-CN”是“zh-Hans-CN”区域性的别名,“zh-TW”是“zh-Hant-TW”区域性的别名。 对 GetCultures 方法的调用不会返回别名区域性,并且可能具有与 Windows 对应项不同的属性值(包括不同的 Parent 区域性)。
zh-CN 和 zh-TW 文化中的这些差异包括以下内容:
在 Windows 系统上,“zh-CN”文化的父文化为“zh-Hans”,“zh-TW”文化的父文化为“zh-Hant”。 这些文化的母文化是“zh”。 在Unix系统中,两种文化的父区域都是“zh”。 这意味着,如果没有为“zh-CN”或“zh-TW”区域提供特定于区域的资源,但为中立区域“zh-Hans”或“zh-Hant”提供了资源,那么应用程序将在 Windows 上加载中立区域的资源,但在 Unix 上不会。 在 Unix 系统上,必须将线程的 CurrentUICulture 显式设置为“zh-Hans”或“zh-Hant”。
在 Windows 系统上,在表示“zh-CN”区域性的实例上调用 CultureInfo.Equals 并向其传递“zh-Hans-CN”实例将返回
true。 在 Unix 系统上,该方法调用返回false。 此行为也适用于在“zh-TW” Equals 实例上调用 CultureInfo 并将其传递给“zh-Hant-Tw”实例。
动态文化数据
除了不变文化外,文化数据是动态的。 即使预定义文化也是如此。 例如,国家或地区采用新货币、更改字词的拼写或者更改首选日历,文化定义也会进行调整以跟踪这些变化。 自定义区域性可能随时更改而不另行通知,并且任何特定区域性都可能被自定义替代区域性覆盖。 此外,如下所述,单个用户可以覆盖文化偏好。 应用程序应始终在运行时获取区域性数据。
Caution
保存数据时,应用程序应使用不变的区域性、二进制格式或与区域性无关的特定格式。 根据与特定区域性(而非不变区域性)关联的当前值保存的数据,如果该区域性发生更改,可能会变得难以读取或其含义可能会改变。
当前文化和当前用户界面文化
.NET 应用程序中的每个线程都具有当前文化和当前 UI 文化。 当前文化决定日期、时间、数字和货币值的格式设置约定,文本排序顺序,大小写约定,以及字符串的比较方式。 当前的 UI 文化用于在运行时检索特定文化的资源。
注释
有关如何按线程确定当前区域性和当前 UI 区域性的详细信息,请参阅“区域性和线程”部分。 有关如何在执行基于任务的异步操作的线程上确定当前文化和 UI 文化的信息,请参阅“文化和基于任务的异步操作”部分。
有关当前区域性的详细信息,请参阅 CultureInfo.CurrentCulture 属性。 有关当前 UI 文化的详细信息,请参阅 CultureInfo.CurrentUICulture 属性主题。
检索当前文化和当前 UI 文化
可以通过以下两种方式之一获取与当前文化相关的 CultureInfo 对象:
- 通过检索 CultureInfo.CurrentCulture 属性的值。
- 通过检索 Thread.CurrentThread.CurrentCulture 属性的值。
以下示例检索到这两个属性值,并将它们进行比较以显示它们相等,并显示当前文化的名称。
using System;
using System.Globalization;
using System.Threading;
public class CurrentCultureEx
{
public static void Main()
{
CultureInfo culture1 = CultureInfo.CurrentCulture;
CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
Console.WriteLine($"The current culture is {culture1.Name}");
Console.WriteLine($"The two CultureInfo objects are equal: {culture1 == culture2}");
}
}
// The example displays output like the following:
// The current culture is en-US
// The two CultureInfo objects are equal: True
可以通过以下两种方式之一获取表示当前 UI 文化的 CultureInfo 对象:
通过检索 CultureInfo.CurrentUICulture 属性的值。
通过检索 Thread.CurrentThread.CurrentUICulture 属性的值。
以下示例检索了这两个属性值,对其进行比较以显示它们是否相等,并显示了当前 UI 区域性的名称。
using System;
using System.Globalization;
using System.Threading;
public class CurrentUIEx
{
public static void Main()
{
CultureInfo uiCulture1 = CultureInfo.CurrentUICulture;
CultureInfo uiCulture2 = Thread.CurrentThread.CurrentUICulture;
Console.WriteLine($"The current UI culture is {uiCulture1.Name}");
Console.WriteLine($"The two CultureInfo objects are equal: {uiCulture1 == uiCulture2}");
}
}
// The example displays output like the following:
// The current UI culture is en-US
// The two CultureInfo objects are equal: True
设置当前区域性和当前 UI 区域性
若要更改线程的区域性和 UI 区域性,请执行以下操作:
通过调用 CultureInfo 类构造函数并向其传递区域性的名称,实例化表示该区域性的 CultureInfo 对象。 如果新区域性与当前 Windows 区域性相同,CultureInfo(String) 构造函数将实例化反映用户替代的 CultureInfo 对象。 可以使用 CultureInfo(String, Boolean) 构造函数来指定当新区域性与当前 Windows 区域性相同时,新实例化的 CultureInfo 对象是否应反映用户的替代。
将 CultureInfo 对象分配给 CultureInfo.CurrentCulture 或 CultureInfo.CurrentUICulture 属性。
以下示例检索当前文化设置。 如果是法语(法国)以外的区域性,则会将当前区域性更改为法语(法国)。 否则,它会将当前文化更改为法语(卢森堡)。
using System;
using System.Globalization;
public class ChangeEx1
{
public static void Main()
{
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
if (current.Name.Equals("fr-FR"))
newCulture = new CultureInfo("fr-LU");
else
newCulture = new CultureInfo("fr-FR");
CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
}
}
// The example displays output like the following:
// The current culture is en-US
// The current culture is now fr-FR
以下示例检索当前文化设置。 如果是斯洛文尼亚语(斯洛文尼亚)以外的区域性,则会将目前的区域性更改为斯洛文尼亚语(斯洛文尼亚)。 否则,它会将当前的文化设置更改为克罗地亚文化设置。
using System;
using System.Globalization;
public class ChangeUICultureEx
{
public static void Main()
{
CultureInfo current = CultureInfo.CurrentUICulture;
Console.WriteLine($"The current UI culture is {current.Name}");
CultureInfo newUICulture;
if (current.Name.Equals("sl-SI"))
newUICulture = new CultureInfo("hr-HR");
else
newUICulture = new CultureInfo("sl-SI");
CultureInfo.CurrentUICulture = newUICulture;
Console.WriteLine($"The current UI culture is now {CultureInfo.CurrentUICulture.Name}");
}
}
// The example displays output like the following:
// The current UI culture is en-US
// The current UI culture is now sl-SI
获取所有文化
可以通过调用 GetCultures 方法来检索一组特定类别的区域性,或本地计算机上可用的所有区域性。 例如,您可以单独检索自定义区域性、特定区域性或中性区域性,或者组合检索。
以下示例调用了两次 GetCultures 方法,首先使用 System.Globalization.CultureTypes 枚举成员检索所有自定义区域性,然后使用 System.Globalization.CultureTypes 枚举成员检索所有替换区域性。
using System;
using System.Globalization;
public class GetCulturesEx
{
public static void Main()
{
// Get all custom cultures.
CultureInfo[] custom = CultureInfo.GetCultures(CultureTypes.UserCustomCulture);
if (custom.Length == 0)
{
Console.WriteLine("There are no user-defined custom cultures.");
}
else
{
Console.WriteLine("Custom cultures:");
foreach (var culture in custom)
Console.WriteLine($" {culture.Name} -- {culture.DisplayName}");
}
Console.WriteLine();
// Get all replacement cultures.
CultureInfo[] replacements = CultureInfo.GetCultures(CultureTypes.ReplacementCultures);
if (replacements.Length == 0)
{
Console.WriteLine("There are no replacement cultures.");
}
else
{
Console.WriteLine("Replacement cultures:");
foreach (var culture in replacements)
Console.WriteLine($" {culture.Name} -- {culture.DisplayName}");
}
Console.WriteLine();
}
}
// The example displays output like the following:
// Custom cultures:
// x-en-US-sample -- English (United States)
// fj-FJ -- Boumaa Fijian (Viti)
//
// There are no replacement cultures.
文化和线程
启动新的应用程序线程时,其当前区域性和当前 UI 区域性由当前系统区域性定义,而不是由当前线程区域性定义。 以下示例演示了差异。 它将应用程序线程的当前文化和当前 UI 文化设置为法语(法国)文化 (fr-FR)。 如果当前区域性已经是 fr-FR,则示例将其设置为英语(美国)区域性 (en-US)。 它将三个随机数显示为货币值,然后创建一个新线程,再将另外三个随机数显示为货币值。 但是,如示例中的输出所示,新线程显示的货币数值并未体现法语(法国)文化的格式规范,这与主线程的输出有所不同。
using System;
using System.Globalization;
using System.Threading;
public class DefaultThreadEx
{
static Random rnd = new Random();
public static void Main()
{
if (Thread.CurrentThread.CurrentCulture.Name != "fr-FR")
{
// If current culture is not fr-FR, set culture to fr-FR.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
}
else
{
// Set culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
}
ThreadProc();
Thread worker = new Thread(ThreadProc);
worker.Name = "WorkerThread";
worker.Start();
}
private static void DisplayThreadInfo()
{
Console.WriteLine($"\nCurrent Thread Name: '{Thread.CurrentThread.Name}'");
Console.WriteLine($"Current Thread Culture/UI Culture: {Thread.CurrentThread.CurrentCulture.Name}/{Thread.CurrentThread.CurrentUICulture.Name}");
}
private static void DisplayValues()
{
// Create new thread and display three random numbers.
Console.WriteLine("Some currency values:");
for (int ctr = 0; ctr <= 3; ctr++)
Console.WriteLine($" {rnd.NextDouble() * 10:C2}");
}
private static void ThreadProc()
{
DisplayThreadInfo();
DisplayValues();
}
}
// The example displays output similar to the following:
// Current Thread Name: ''
// Current Thread Culture/UI Culture: fr-FR/fr-FR
// Some currency values:
// 8,11 €
// 1,48 €
// 8,99 €
// 9,04 €
//
// Current Thread Name: 'WorkerThread'
// Current Thread Culture/UI Culture: en-US/en-US
// Some currency values:
// $6.72
// $6.35
// $2.90
// $7.72
可以通过将表示该区域性的 CultureInfo 对象分配给 DefaultThreadCurrentCulture 和 DefaultThreadCurrentUICulture 属性来设置应用程序域中所有线程的区域性和 UI 区域性。 以下示例利用这些属性确保默认应用程序域中的所有线程共享相同的文化设置。
using System;
using System.Globalization;
using System.Threading;
public class SetThreadsEx
{
static Random rnd = new Random();
public static void Main()
{
if (Thread.CurrentThread.CurrentCulture.Name != "fr-FR")
{
// If current culture is not fr-FR, set culture to fr-FR.
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
}
else
{
// Set culture to en-US.
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
}
ThreadProc();
Thread worker = new Thread(SetThreadsEx.ThreadProc);
worker.Name = "WorkerThread";
worker.Start();
}
private static void DisplayThreadInfo()
{
Console.WriteLine($"\nCurrent Thread Name: '{Thread.CurrentThread.Name}'");
Console.WriteLine($"Current Thread Culture/UI Culture: {Thread.CurrentThread.CurrentCulture.Name}/{Thread.CurrentThread.CurrentUICulture.Name}");
}
private static void DisplayValues()
{
// Create new thread and display three random numbers.
Console.WriteLine("Some currency values:");
for (int ctr = 0; ctr <= 3; ctr++)
Console.WriteLine($" {rnd.NextDouble() * 10:C2}");
}
private static void ThreadProc()
{
DisplayThreadInfo();
DisplayValues();
}
}
// The example displays output similar to the following:
// Current Thread Name: ''
// Current Thread Culture/UI Culture: fr-FR/fr-FR
// Some currency values:
// 6,83 €
// 3,47 €
// 6,07 €
// 1,70 €
//
// Current Thread Name: 'WorkerThread'
// Current Thread Culture/UI Culture: fr-FR/fr-FR
// Some currency values:
// 9,54 €
// 9,50 €
// 0,58 €
// 6,91 €
将值分配给 DefaultThreadCurrentCulture 和 DefaultThreadCurrentUICulture 属性时,如果应用程序域中线程的区域性和 UI 区域性尚未显式分配给区域性,则它们也会更改。 但是,这些线程仅在当前应用程序域中执行时反映新的文化设置。 如果这些线程在另一个应用程序域中执行,则其区域性将成为为该应用程序域定义的默认区域性。 因此,建议始终设置主应用程序线程的区域性,而不要依靠 DefaultThreadCurrentCulture 和 DefaultThreadCurrentUICulture 属性来更改它。
文化和基于任务的异步操作
基于任务的异步编程模式使用 Task 和 Task<TResult> 对象在线程池线程上异步执行委托。 无法事先知道特定任务在哪个线程运行,只有在运行时才能确定。
区域性是异步操作上下文的一部分。 换句话说,异步操作默认继承启动该操作的线程的 CurrentCulture 和 CurrentUICulture 属性的值。 如果当前区域性或当前 UI 区域性与系统区域性不同,则当前区域性会跨越线程边界,并成为正在执行异步操作的线程池线程的当前区域性。
下面的示例提供了简单的演示。 该示例定义一个 Func<TResult> 委托,formatDelegate,该委托返回一些格式设置为货币值的数字。 本示例将当前系统文化更改为法语(法国),如果法语(法国)已是当前系统文化,则改为英语(美国)。 然后:
- 直接调用委托,以便它在主应用线程上同步运行。
- 创建在线程池线程上异步执行委托的任务。
- 通过调用 Task.RunSynchronously 方法,创建在主应用线程上同步执行委托的任务。
如示例中的输出所示,当前区域性更改为法语(法国)时,从中异步调用任务的线程的当前区域性将成为该异步操作的当前区域性。
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
public class AsyncCultureEx1
{
public static void Main()
{
decimal[] values = { 163025412.32m, 18905365.59m };
string formatString = "C2";
string FormatDelegate()
{
string output = $"Formatting using the {CultureInfo.CurrentCulture.Name} " +
$"culture on thread {Thread.CurrentThread.ManagedThreadId}.\n";
foreach (decimal value in values)
output += $"{value.ToString(formatString)} ";
output += Environment.NewLine;
return output;
}
Console.WriteLine($"The example is running on thread {Thread.CurrentThread.ManagedThreadId}");
// Make the current culture different from the system culture.
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}");
if (CultureInfo.CurrentCulture.Name == "fr-FR")
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
else
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Console.WriteLine($"Changed the current culture to {CultureInfo.CurrentCulture.Name}.\n");
// Execute the delegate synchronously.
Console.WriteLine("Executing the delegate synchronously:");
Console.WriteLine(FormatDelegate());
// Call an async delegate to format the values using one format string.
Console.WriteLine("Executing a task asynchronously:");
var t1 = Task.Run(FormatDelegate);
Console.WriteLine(t1.Result);
Console.WriteLine("Executing a task synchronously:");
var t2 = new Task<string>(FormatDelegate);
t2.RunSynchronously();
Console.WriteLine(t2.Result);
}
}
// The example displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the fr-FR culture on thread 3.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
CultureInfo 对象序列化
序列化 CultureInfo 对象时,实际存储的所有对象都是 Name 和 UseUserOverride。 它只有在 Name 具有相同语义的环境中才能成功反序列化。 以下三个示例演示了为何情况并非总是如此:
如果 CultureTypes 属性值为 CultureTypes.InstalledWin32Cultures,并且该区域设置是首次在特定版本的 Windows 操作系统中引入的,则无法在较早版本的 Windows 上对其进行反序列化。 例如,如果在 Windows 10 上添加了一种区域性,那么就无法在 Windows 8 上反序列化它。
如果 CultureTypes 值是 CultureTypes.UserCustomCulture,并且执行反序列化的计算机上未安装此用户自定义文化,则无法进行反序列化。
如果 CultureTypes 值为 CultureTypes.ReplacementCultures,并且用于反序列化的计算机没有此替代区域性,则它将反序列化为相同名称,但并非所有特征都相同。 例如,如果
en-US是计算机 A 上的替换区域性,但在计算机 B 上不是替换区域性,并且在计算机 A 上对引用该区域性的 CultureInfo 对象进行了序列化,然后在计算机 B 上进行反序列化,则该区域性的任何自定义特征都不会被传递。 文化数据已成功反序列化,但含义有所不同。
控制面板替代
用户可能会选择通过控制面板的区域和语言选项替代与 Windows 当前区域性相关的一些值。 例如,用户可能选择以不同的格式显示日期,或使用区域性默认值以外的货币。 通常,你的应用程序应尊重这些用户覆盖设置。
如果 UseUserOverride 是 true 且指定区域性与 Windows 的当前区域性匹配,则 CultureInfo 应用这些重载,包括 DateTimeFormatInfo 属性返回的 DateTimeFormat 实例属性的用户设置,以及 NumberFormatInfo 属性返回的 NumberFormat 实例的属性。 如果用户设置与 CultureInfo 关联的文化不兼容,例如,如果所选日历不是 OptionalCalendars 中的一种,则方法的结果和属性的值将不确定。
替换排序顺序
某些文化支持多种排序顺序。 例如:
- 西班牙(西班牙)文化有两种排序顺序:默认的国际排序顺序和传统排序顺序。 使用 CultureInfo 文化名称实例化
es-ES对象时,将使用国际排序。 使用 CultureInfo 文化名称实例化es-ES-tradnl对象时,会使用传统排序顺序。 -
zh-CN(简体中文,中国)区域性支持两种排序顺序:按发音排序(默认)和按笔画数排序。 使用 CultureInfo 区域性名称实例化zh-CN对象时,将使用默认排序顺序。 实例化具有本地标识符 0x00020804 的 CultureInfo 对象时,字符串按笔画数排序。
下表列出了支持备用排序顺序的区域性以及默认排序顺序和备用排序顺序的标识符。
| 文化名称 | 文化 | 默认排序名称和标识符 | 备用排序名称和标识符 |
|---|---|---|---|
| es-ES | 西班牙语(西班牙) | 国际:0x00000C0A | 传统:0x0000040A |
| zh-TW | 中文(台湾) | 笔画数:0x00000404 | 注音符号: 0x00030404 |
| zh-CN | 中文(中华人民共和国) | 发音:0x00000804 | 笔画数:0x00020804 |
| zh-HK | 中文(香港特别行政区) | 笔画数:0x00000c04 | 笔画数:0x00020c04 |
| zh-SG | 中文(新加坡) | 发音:0x00001004 | 笔画数:0x00021004 |
| zh-MO | 中文(澳门特别行政区) | 发音:0x00001404 | 笔画数:0x00021404 |
| ja-JP | 日语(日本) | 默认:0x00000411 | Unicode:0x00010411 |
| ko-KR | 韩语(韩国) | 默认:0x00000412 | 朝鲜语 Xwansung - Unicode:0x00010412 |
| de-DE | 德语(德国) | 字典:0x00000407 | 电话簿排序 DIN:0x00010407 |
| hu-HU | 匈牙利语(匈牙利) | 默认:0x0000040e | 技术排序:0x0001040e |
| ka-GE | 格鲁吉亚语(格鲁吉亚) | 传统:0x00000437 | 现代排序:0x00010437 |
示例
以下示例演示如何使用国际排序创建 CultureInfo 西班牙语(西班牙)的对象,以及具有传统排序的另一个 CultureInfo 对象。
using System;
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
*/
Imports System.Collections
Imports System.Globalization
Module Module1
Public Sub Main()
' Creates and initializes the CultureInfo which uses the international sort.
Dim myCIintl As New CultureInfo("es-ES", False)
' Creates and initializes the CultureInfo which uses the traditional sort.
Dim myCItrad As New CultureInfo(&H40A, 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"))
End Sub
'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
End Module
构造函数
| 名称 | 说明 |
|---|---|
| CultureInfo(Int32, Boolean) |
根据区域性标识符指定的区域性以及指定是否使用 Windows 中的用户所选区域性设置的值初始化类的新实例 CultureInfo 。 |
| CultureInfo(Int32) |
根据区域性标识符指定的区域性初始化类的新实例 CultureInfo 。 |
| CultureInfo(String, Boolean) |
根据名称指定的区域性和指定值初始化类的新实例 CultureInfo ,该值指定是否使用 Windows 中的用户所选区域性设置。 |
| CultureInfo(String) |
根据名称指定的区域性初始化类的新实例 CultureInfo 。 |
属性
| 名称 | 说明 |
|---|---|
| Calendar |
获取区域性使用的默认日历。 |
| CompareInfo |
CompareInfo获取定义如何比较区域性的字符串。 |
| CultureTypes |
获取与当前 CultureInfo 对象相关的区域性类型。 |
| CurrentCulture |
获取或设置 CultureInfo 表示当前线程和基于任务的异步操作使用的区域性的对象。 |
| CurrentUICulture |
获取或设置 CultureInfo 对象,该对象表示资源管理器用于在运行时查找区域性特定资源的当前用户界面区域性。 |
| DateTimeFormat |
获取或设置一个 DateTimeFormatInfo 定义显示日期和时间的文化适当格式。 |
| DefaultThreadCurrentCulture |
获取或设置当前应用程序域中线程的默认区域性。 |
| DefaultThreadCurrentUICulture |
获取或设置当前应用程序域中线程的默认 UI 区域性。 |
| DisplayName |
获取完整的本地化区域性名称。 |
| EnglishName |
获取采用 英语语言 语言格式 [country/regionfull] 的区域性名称。 |
| IetfLanguageTag |
已弃用。 获取语言的 RFC 4646 标准标识。 |
| InstalledUICulture |
CultureInfo获取表示随操作系统一起安装的区域性。 |
| InvariantCulture |
CultureInfo获取与区域性无关的对象(固定)。 |
| IsNeutralCulture |
获取一个值,该值指示当前 CultureInfo 是否表示非特定区域性。 |
| IsReadOnly |
获取一个值,该值指示当前 CultureInfo 是否为只读。 |
| KeyboardLayoutId |
获取活动输入区域设置标识符。 |
| LCID |
获取当前 CultureInfo区域性标识符。 |
| Name |
获取格式为 languagecode2- 的区域性名称。 |
| NativeName |
获取区域性名称,由语言、国家/地区和可选脚本组成,区域性设置为显示。 |
| NumberFormat |
获取或设置一个 NumberFormatInfo 定义显示数字、货币和百分比的文化适当格式。 |
| OptionalCalendars |
获取区域性可以使用的日历列表。 |
| Parent |
获取表示 CultureInfo 当前 CultureInfo区域性的父区域性。 |
| TextInfo |
TextInfo获取定义与区域性关联的写入系统。 |
| ThreeLetterISOLanguageName |
获取当前 CultureInfo语言的 ISO 639-2 三字母代码。 |
| ThreeLetterWindowsLanguageName |
获取Windows API 中定义的语言的三字母代码。 |
| TwoLetterISOLanguageName |
获取当前 CultureInfo语言的 ISO 639-1 双字母或 ISO 639-3 三字母代码。 |
| UseUserOverride |
获取一个值,该值指示当前 CultureInfo 对象是否使用用户选择的区域性设置。 |
方法
| 名称 | 说明 |
|---|---|
| ClearCachedData() |
刷新缓存的区域性相关信息。 |
| Clone() |
创建当前 CultureInfo的副本。 |
| CreateSpecificCulture(String) |
创建一个 CultureInfo 表示与指定名称关联的特定区域性。 |
| Equals(Object) |
确定指定的对象是否与当前 CultureInfo区域性相同。 |
| GetConsoleFallbackUICulture() |
获取适用于控制台应用程序的备用用户界面区域性,当默认图形用户界面区域性不适合时。 |
| GetCultureInfo(Int32) |
使用指定的区域性标识符检索区域性的缓存只读实例。 |
| GetCultureInfo(String, Boolean) |
检索区域性的缓存只读实例。 |
| GetCultureInfo(String, String) |
检索区域性的缓存只读实例。 参数指定使用另一区域性指定的对象TextInfo初始化CompareInfo的区域性。 |
| GetCultureInfo(String) |
使用指定的区域性名称检索区域性的缓存只读实例。 |
| GetCultureInfoByIetfLanguageTag(String) |
已弃用。 检索具有由指定 RFC 4646 语言标记标识的语言特征的只读 CultureInfo 对象。 |
| GetCultures(CultureTypes) |
获取受指定 CultureTypes 参数筛选的受支持区域性的列表。 |
| GetFormat(Type) |
获取一个对象,该对象定义如何设置指定类型的格式。 |
| GetHashCode() |
用作当前 CultureInfo哈希函数,适合哈希算法和数据结构,例如哈希表。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ReadOnly(CultureInfo) |
返回指定 CultureInfo 对象周围的只读包装器。 |
| ToString() |
返回一个字符串,该字符串包含格式CultureInfo国家/地区code2- 的当前名称。 |