Page.Culture 对比 Thread.CurrentThread.CurrentCulture

Page.Culture vs Thread.CurrentThread.CurrentCulture

我一直在使用 Thread.CurrentThread.CurrentUICultureSystem.ThreadingSystem.Globalization,现在有一段时间手动设置我的 ASP.net 页面使用的语言,主要是 WebForms 和 WebPages用剃刀。

MSDN: Thread.CurrentThread.CurrentUICulture

我最近读了一个使用 Page.UICulture 的教程(实际上,UICulture 似乎是强类型的)。令我惊讶的是,我得到了完全相同的结果;他们都更改了我网站的 ui 语言设置并读取了正确的资源文件。

MSDN: Page.UICulture

对我来说,Thread.CurrentUICulture 更有意义(我说的是 intui,因为它的字面意思是 "changing the culture of the current thread")。

但是调用 Page.Culture 更容易,并且不需要 ui 调用另一对 ASP.net using,所以我已经接受了现在的解决方案。

两者之间有根本区别还是完全可以互换?

之所以担心,是因为我有一堆用第一种方法开发的旧网站,如果贸然更新到第二种方法,我怕运行发生互换冲突。

注意:我的工作通常专注于UICulture,而Culture是我所做工作的附属品,但我想问他们两个的问题。

他们完全做同样的事情。

正如您在 documentation page 上看到的:

This property is a shortcut for the CurrentThread property. The culture is a property of the executing thread

Page.UICultureThread.CurrentThread 属性 的包装器,是供内部 .NET 框架使用的工具:

This property is a shortcut for the CurrentThread property. The culture is a property of the executing thread

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

查看source code,你可以清楚地看到:

public string UICulture 
{
    set 
    {
        CultureInfo newUICulture = null;

        if(StringUtil.EqualsIgnoreCase(value, HttpApplication.AutoCulture))
        {
            CultureInfo browserCulture = CultureFromUserLanguages(false);
            if(browserCulture != null) 
            {
                newUICulture = browserCulture;
            }
        }
        else if(StringUtil.StringStartsWithIgnoreCase(value, HttpApplication.AutoCulture)) 
        {
            CultureInfo browserCulture = CultureFromUserLanguages(false);
            if(browserCulture != null) 
            {
                newUICulture = browserCulture;
            }
            else
            {
                try 
                {
                    newUICulture = HttpServerUtility.CreateReadOnlyCultureInfo(value.Substring(5));
                }
                catch {}
            }
        }
        else
        {
            newUICulture = HttpServerUtility.CreateReadOnlyCultureInfo(value);
        }

        if (newUICulture != null) 
        {
            Thread.CurrentThread.CurrentUICulture = newUICulture;
            _dynamicUICulture = newUICulture;
        }
    }
    get { return Thread.CurrentThread.CurrentUICulture.DisplayName; }
}