相关文章推荐
有腹肌的卡布奇诺  ·  C# ...·  10 月前    · 
玩命的小虾米  ·  基于 D3.js 的 SVG ...·  1 年前    · 
腹黑的橡皮擦  ·  FAQ - Sun Packages·  1 年前    · 
public:
 static property System::Globalization::CultureInfo ^ CurrentCulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public:
 static property System::Globalization::CultureInfo ^ CurrentCulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentCulture { get; set; }
public static System.Globalization.CultureInfo CurrentCulture { get; }
member this.CurrentCulture : System.Globalization.CultureInfo with get, set
member this.CurrentCulture : System.Globalization.CultureInfo
Public Shared Property CurrentCulture As CultureInfo
Public Shared ReadOnly Property CurrentCulture As CultureInfo
using namespace System; using namespace System::Globalization; using namespace System::Threading; int main() // Display the name of the current thread culture. Console::WriteLine("CurrentCulture is {0}.", CultureInfo::CurrentCulture->Name); // Change the current culture to th-TH. CultureInfo::CurrentCulture = gcnew CultureInfo("th-TH",false); Console::WriteLine("CurrentCulture is now {0}.", CultureInfo::CurrentCulture->Name); // Displays the name of the CurrentUICulture of the current thread. Console::WriteLine("CurrentUICulture is {0}.", CultureInfo::CurrentCulture->Name); // Changes the CurrentUICulture of the current thread to ja-JP. CultureInfo::CurrentUICulture = gcnew CultureInfo("ja-JP",false); Console::WriteLine("CurrentUICulture is now {0}.", CultureInfo::CurrentCulture->Name); // The example displays the following output: // CurrentCulture is en-US. // CurrentCulture is now th-TH. // CurrentUICulture is en-US. // CurrentUICulture is now ja-JP. using System; using System.Globalization; using System.Threading; public class Example public static void Main() // Display the name of the current culture. Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name); // Change the current culture to th-TH. CultureInfo.CurrentCulture = new CultureInfo("th-TH", false); Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name); // Display the name of the current UI culture. Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name); // Change the current UI culture to ja-JP. CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false ); Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name); // The example displays the following output: // CurrentCulture is en-US. // CurrentCulture is now th-TH. // CurrentUICulture is en-US. // CurrentUICulture is now ja-JP. Imports System.Globalization Imports System.Threading Public Module Example Public Sub Main() ' Display the name of the current culture. Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name) ' Change the current culture to th-TH. CultureInfo.CurrentCulture = New CultureInfo("th-TH", False) Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP. CultureInfo.CurrentUICulture = New CultureInfo("ja-JP", False) Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name) End Sub End Module ' The example displays the following output: ' CurrentCulture is en-US. ' CurrentCulture is now th-TH. ' CurrentUICulture is en-US. ' CurrentUICulture is now ja-JP.

此属性返回的对象及其关联的对象确定日期、时间、数字、货币值、文本排序顺序、大小写约定和字符串比较 CultureInfo 的默认格式。

当前区域性是执行线程的属性。 将此属性设置为表示新区域性的 对象时,属性的值 CultureInfo Thread.CurrentThread.CurrentCulture 也会更改。 但是,我们建议始终使用 CultureInfo.CurrentCulture 属性来检索和设置当前区域性。

CultureInfo 此属性返回的对象是只读的。 这意味着不能更改现有 对象,例如,通过更改 DateTimeFormat 。 若要更改日期时间格式或当前区域性的某个其他方面,请创建一个新的 对象 CultureInfo 并将其分配给 属性。

在 .NET Framework 4.5.2 及更早版本中,属性是只读的;也就是说,可以检索属性值,但不能 CultureInfo.CurrentCulture 设置它。

本节内容:

如何确定线程的区域性
获取当前区域性
显式设置 CurrentCulture 属性
区域性和Windows应用

如何确定线程的区域性

启动线程时,其区域性最初确定如下:

  • 通过在 DefaultThreadCurrentCulture 执行线程的应用程序域中检索属性指定的区域性,如果属性值不为 null

  • 如果该线程是一个线程池线程,该线程正在执行基于任务的异步操作,而该应用面向 .NET Framework 4.6 或更高版本的 .NET Framework,则其区域性由调用线程的区域性确定。 下面的示例将当前区域性更改为葡萄牙 (巴西) 并启动六个任务,每个任务显示其线程 ID、任务 ID 及其当前区域性。 每个任务 (和) 的线程继承了调用线程的区域性。

    using System; using System.Collections.Generic; using System.Globalization; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; [assembly:TargetFramework(".NETFramework,Version=v4.6")] public class Example public static async Task Main() var tasks = new List<Task>(); Console.WriteLine("The current culture is {0}", Thread.CurrentThread.CurrentCulture.Name); Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR"); // Change the current culture to Portuguese (Brazil). Console.WriteLine("Current culture changed to {0}", Thread.CurrentThread.CurrentCulture.Name); Console.WriteLine("Application thread is thread {0}", Thread.CurrentThread.ManagedThreadId); // Launch six tasks and display their current culture. for (int ctr = 0; ctr <= 5; ctr++) tasks.Add(Task.Run( () => { Console.WriteLine("Culture of task {0} on thread {1} is {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.CurrentCulture.Name); } )); await Task.WhenAll(tasks.ToArray()); // The example displays output like the following: // The current culture is en-US // Current culture changed to pt-BR // Application thread is thread 9 // Culture of task 2 on thread 11 is pt-BR // Culture of task 1 on thread 10 is pt-BR // Culture of task 3 on thread 11 is pt-BR // Culture of task 5 on thread 11 is pt-BR // Culture of task 6 on thread 11 is pt-BR // Culture of task 4 on thread 10 is pt-BR Imports System.Collections.Generic Imports System.Globalization Imports System.Runtime.Versioning Imports System.Threading Imports System.Threading.Tasks <assembly:TargetFramework(".NETFramework,Version=v4.6")> Module Example Public Sub Main() Dim tasks As New List(Of Task) Console.WriteLine("The current culture is {0}", Thread.CurrentThread.CurrentCulture.Name) Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-BR") ' Change the current culture to Portuguese (Brazil). Console.WriteLine("Current culture changed to {0}", Thread.CurrentThread.CurrentCulture.Name) Console.WriteLine("Application thread is thread {0}", Thread.CurrentThread.ManagedThreadId) ' Launch six tasks and display their current culture. For ctr As Integer = 0 to 5 tasks.Add(Task.Run(Sub() Console.WriteLine("Culture of task {0} on thread {1} is {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.CurrentCulture.Name) End Sub)) Task.WaitAll(tasks.ToArray()) End Sub End Module ' The example displays output like the following: ' The current culture is en-US ' Current culture changed to pt-BR ' Application thread is thread 9 ' Culture of task 2 on thread 11 is pt-BR ' Culture of task 1 on thread 10 is pt-BR ' Culture of task 3 on thread 11 is pt-BR ' Culture of task 5 on thread 11 is pt-BR ' Culture of task 6 on thread 11 is pt-BR ' Culture of task 4 on thread 10 is pt-BR

    有关详细信息,请参阅主题中的 "区域性和基于任务的异步操作" 一节 CultureInfo

  • 通过调用 GetUserDefaultLocaleName Windows 上的函数或 uloc_getDefault ICU 调用函数,该函数当前 setlocale LC_MESSAGES 在类似于 Unix 的系统上使用 category 调用 POSIX 函数。

    请注意,如果您设置的特定区域性与系统安装的区域性或用户的首选区域性不同,并且您的应用程序将启动多个线程,则这些线程的当前区域性将是函数返回的区域性 GetUserDefaultLocaleName ,除非您在 DefaultThreadCurrentCulture 执行线程的应用程序域中将区域性分配给属性。

    有关如何确定如何确定线程的区域性的详细信息,请参阅 "引用" 页中的 "区域性和线程" 一节 CultureInfo

    获取当前区域性

    CultureInfo.CurrentCulture 属性是每个线程的设置; 也就是说,每个线程都可以有自己的区域性。 可以通过检索属性的值获取当前线程的区域性 CultureInfo.CurrentCulture ,如下面的示例所示。

    using System; using System.Globalization; public class Example public static void Main() CultureInfo culture = CultureInfo.CurrentCulture; Console.WriteLine("The current culture is {0} [{1}]", culture.NativeName, culture.Name); // The example displays output like the following: // The current culture is English (United States) [en-US] Imports System.Globalization Module Example Public Sub Main() Dim culture As CultureInfo = CultureInfo.CurrentCulture Console.WriteLine("The current culture is {0} [{1}]", culture.NativeName, culture.Name) End Sub End Module ' The example displays output like the following: ' The current culture is English (United States) [en-US]

    显式设置 CurrentCulture 属性

    若要更改现有线程使用的区域性,请将 CultureInfo.CurrentCulture 属性设置为新的区域性。 如果以这种方式显式更改线程的区域性,则当线程跨越应用程序域边界时,此更改将保持不变。 下面的示例将当前线程区域性更改为荷兰 (荷兰) 。 它还显示,当当前线程跨越应用程序域边界时,它的当前区域性保持更改。

    using System; using System.Globalization; using System.Threading; public class Info : MarshalByRefObject public void ShowCurrentCulture() Console.WriteLine("Culture of {0} in application domain {1}: {2}", Thread.CurrentThread.Name, AppDomain.CurrentDomain.FriendlyName, CultureInfo.CurrentCulture.Name); public class Example public static void Main() Info inf = new Info(); // Set the current culture to Dutch (Netherlands). Thread.CurrentThread.Name = "MainThread"; CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL"); inf.ShowCurrentCulture(); // Create a new application domain. AppDomain ad = AppDomain.CreateDomain("Domain2"); Info inf2 = (Info) ad.CreateInstanceAndUnwrap(typeof(Info).Assembly.FullName, "Info"); inf2.ShowCurrentCulture(); // The example displays the following output: // Culture of MainThread in application domain ChangeCulture1.exe: nl-NL // Culture of MainThread in application domain Domain2: nl-NL Imports System.Globalization Imports System.Threading Public Class Info : Inherits MarshalByRefObject Public Sub ShowCurrentCulture() Console.WriteLine("Culture of {0} in application domain {1}: {2}", Thread.CurrentThread.Name, AppDomain.CurrentDomain.FriendlyName, CultureInfo.CurrentCulture.Name) End Sub End Class Module Example Public Sub Main() Dim inf As New Info() ' Set the current culture to Dutch (Netherlands). Thread.CurrentThread.Name = "MainThread" CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL") inf.ShowCurrentCulture() ' Create a new application domain. Dim ad As AppDomain = AppDomain.CreateDomain("Domain2") Dim inf2 As Info = CType(ad.CreateInstanceAndUnwrap(GetType(Info).Assembly.FullName, "Info"), Info) inf2.ShowCurrentCulture() End Sub End Module ' This example displays the following output: ' Culture of MainThread in application domain Example.exe: nl-NL ' Culture of MainThread in application domain Domain2: nl-NL

    使用属性更改区域性 CultureInfo.CurrentCulture 需要 SecurityPermission 具有 ControlThread 值集的权限。 由于与线程关联的安全状态,因此操作线程是危险的。 因此,只应为可信代码提供此权限,并在必要时提供此权限。 不能在不完全受信任的代码中更改线程区域。

    从 .NET Framework 4 开始,可以显式将当前线程区域性更改为特定区域性 (如法语 (加拿大) ) 或非特定区域性 (如法语) 。 当 CultureInfo 对象表示非特定区域性时,属性的值 CultureInfo (如、、、 Calendar 和) CompareInfo DateTimeFormat NumberFormat TextInfo 反映与非特定区域性关联的特定区域性。 例如,英语非特定区域性的主要区域性为英语 (美国) ;德国文化的主要文化是德国 (德国) 。 下面的示例演示了在将当前区域性设置为特定区域性、法语 (加拿大) 以及非特定区域性法语时的格式设置差异。

    using System; using System.Globalization; using System.Threading; public class Example public static void Main() double value = 1634.92; CultureInfo.CurrentCulture = new CultureInfo("fr-CA"); Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name); Console.WriteLine("{0:C2}\n", value); Thread.CurrentThread.CurrentCulture = new CultureInfo("fr"); Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name); Console.WriteLine("{0:C2}", value); // The example displays the following output: // Current Culture: fr-CA // 1 634,92 $ // Current Culture: fr // 1 634,92 € Imports System.Globalization Imports System.Threading Module Example Public Sub Main() Dim value As Double = 1634.92 CultureInfo.CurrentCulture = New CultureInfo("fr-CA") Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name) Console.WriteLine("{0:C2}", value) Console.WriteLine() Thread.CurrentThread.CurrentCulture = New CultureInfo("fr") Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name) Console.WriteLine("{0:C2}", value) End Sub End Module ' The example displays the following output: ' Current Culture: fr-CA ' 1 634,92 $ ' Current Culture: fr ' 1 634,92 €

    你还可以使用 CultureInfo.CurrentCulture 属性以及 HttpRequest.UserLanguages 属性将 CurrentCulture ASP.NET 应用程序的属性设置为用户的首选区域性,如下面的示例所示。

    CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))

    当前区域性和用户重写

    Windows 允许用户 CultureInfo 使用控制面板中的 " 区域和语言选项 " 覆盖对象及其关联对象的标准属性值。 CultureInfo 属性返回的对象 CurrentCulture 在以下情况下将反映这些用户覆盖:

  • 如果当前线程区域性由 Windows 函数隐式设置,则为 GetUserDefaultLocaleName

  • 如果由属性定义的当前线程区域性与 DefaultThreadCurrentCulture 当前 Windows 系统区域性相对应,则为。

  • 如果将当前线程区域性显式设置为方法返回的区域性 CreateSpecificCulture ,并且该区域性与当前 Windows 系统区域性相对应,则为。

  • 如果将当前线程区域性显式设置为构造函数实例化的区域性 CultureInfo(String) ,并且该区域性与当前 Windows 系统区域性相对应,则为。

    在某些情况下,尤其是对于服务器应用程序,可能不需要将当前区域性设置为 CultureInfo 反映用户重写的对象。 相反,可以使用以下方式将当前区域性设置为 CultureInfo 不反映用户重写的对象:

  • 通过 CultureInfo(String, Boolean) 使用参数的值调用构造函数 false useUserOverride

  • 通过调用 GetCultureInfo 方法,该方法返回缓存的只读 CultureInfo 对象。

    当前区域性和 UWP 应用

    在通用 Windows 平台 (UWP) 应用程序中, CurrentCulture 属性是读写的,就像在 .NET Framework 和 .net Core 应用程序中一样,你可以使用它来获取和设置当前区域性。 不过,UWP 应用不区分当前区域性和当前 UI 区域性。 CurrentCulture CurrentUICulture 属性映射到 Windows 中的第一个值 。Windows.applicationmodel.resources.core 集合中的 DefaultContext。

    在 .NET Framework 和 .net Core 应用程序中,当前区域性为每个线程的设置, CurrentCulture 属性仅反映当前线程的区域性。 在 UWP 应用中,当前区域性映射到 Windows。Windows.applicationmodel.resources.core 属性,它是一个全局设置,它是一个全局设置。 设置 CurrentCulture 属性将更改整个应用的区域性; 不能在每个线程的基础上设置区域性。

  •