Thread-CurrentCulture、CurrentUICulture
// As the culture can be customized object then we cannot hold any // reference to it before we check if it is safe because the app domain // owning this customized culture may get unloaded while executing this // code. To achieve that we have to do the check using nativeGetSafeCulture // as the thread cannot get interrupted during the FCALL. // If the culture is safe (not customized or created in current app domain) // then the FCALL will return a reference to that culture otherwise the // FCALL will return failure. In case of failure we'll return the default culture. // If the app domain owning a customized culture that is set to teh thread and this // app domain get unloaded there is a code to clean up the culture from the thread // using the code in AppDomain::ReleaseDomainStores.
//因为文化可以被定制,所以我们不能持有任何
//在检查它是否安全之前引用它,因为应用程序域
//在执行此操作时,拥有此自定义区域性可能会被卸载
//代码。为此,我们必须使用nativeGetSafeCulture进行检查
//因为线程不能在FCALL期间中断。
//如果区域性是安全的(未在当前应用程序域中自定义或创建)
//然后FCALL将返回对该区域性的引用,否则
//FCALL将返回失败。如果失败,我们将返回默认区域性。
//如果应用程序域拥有的自定义区域性设置为线程,并且
//app domain get Unload有一个代码可以从线程中清除区域性
//使用AppDomain::ReleaseDomainStores中的代码。
public CultureInfo CurrentCulture { get { Contract.Ensures(Contract.Result() != null); #if FEATURE_APPX if(AppDomain.IsAppXModel()) { return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentCultureNoAppX(); } else #endif { return GetCurrentCultureNoAppX(); } }
CurrentUICulture
public CultureInfo CurrentUICulture { get { Contract.Ensures(Contract.Result() != null); #if FEATURE_APPX if(AppDomain.IsAppXModel()) { return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentUICultureNoAppX(); } else #endif { return GetCurrentUICultureNoAppX(); } }
class Program
{
static void Main(string[] args)
{
CultureInfo userCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine(userCulture.Name);
Console.WriteLine(userCulture.LCID);
Console.WriteLine(userCulture.NativeName);
Console.WriteLine(userCulture.DisplayName);
Console.WriteLine(userCulture.TwoLetterISOLanguageName);
Console.WriteLine((100000).ToString("c"));
CultureInfo usCulture = Thread.CurrentThread.CurrentUICulture;
Console.WriteLine(usCulture.Name);
Console.WriteLine(usCulture.DisplayName);
Console.ReadLine();
}
}
}
MSDN描述
Thread.CurrentCulture 属性
获取或设置当前线程的区域性。
Thread.CurrentUICulture 属性获取或设置资源管理器使用的当前区域性以便在运行时查找区域性特定的资源。
当从控制面板更改设置后在第一次运行代码时,区域信息并没有改变,当第二次运行时,通过CurrentCulture 属性实例化的userCulture信息发生变化,但是通过CurrentUICulture 属性实例化的usCulture却没有发生变化。
CultureInfo.CurrentUICulture 属性是每个线程的设置,可返回当前用户界面区域性。该属性由 ResourceManager 类用于在运行时查找区域性特定资源。可以使用非特定区域性、特定区域性或 InvariantCulture 来设置 CurrentUICulture 属性。可以使用 Thread.CurrentThread 属性设置 CurrentCulture。
CultureInfo.CurrentCulture 属性是每个线程的设置,可确定日期、时间、货币和数字的默认格式、文本排序顺序、字符串比较以及大小写转换。CurrentCulture 属性不是一个语言设置。它仅包含与地理区域的标准设置相关的数据。因此,只能将 CurrentCulture 属性设置为特定区域性或 InvariantCulture。可以使用 Thread.CurrentThread 属性设置 CurrentCulture。
一句话:当程序运行时,CurrentUICulture属性用来决定加载什么样的资源,而CurrentCulture属性用来决定诸如货币、数字和日期如何格式化。
CurrentCulture属性的默认值是操作系统的User Locale,我们可以在控制面板里设置。
CurrentUICulture属性的默认值是操作系统用户界面语言。
CurrentCulture属性的值必须是一个特定的culture, 比如"fr-FR",不能是一个neutral culture, 比如"fr". 一个neutral culture 例如"fr"会产生问题,因为它适用于所有French语系的culture, 但是France, Belgium和Quebec使用不同的货币。