Search code examples
c#asp.netrazorwebformsasp.net-webpages

Page.Culture vs Thread.CurrentThread.CurrentCulture


I have been using Thread.CurrentThread.CurrentUICulture with System.Threading and System.Globalization, for a while now to manually set the language used by my ASP.net pages, mainly WebForms and WebPages with Razor.

See MSDN: Thread.CurrentThread.CurrentUICulture

I recently read a tutorial that was using Page.UICulture instead (actually, UICulture which appears to be strongly typed). To my surprise I ended up with exactly the same result; they both changed my websites' ui language settings and read the correct resource file.

See MSDN: Page.UICulture

To me, the Thread.CurrentUICulture makes more sense (I say that intuitively, since it's literally "changing the culture of the current thread").

But calling Page.Culture is much easier and doesn't require to call yet another pair of ASP.net using, so I've settled for that solution for now.

Is there a fundamental difference between the two or are they perfectly interchangeable ?

The reason why I worry is that I have a bunch of old websites developed with the first method and I am afraid to run into interchangeability conflicts if I update them to the second one rashly.

Note: I usually focus on UICulture in my line of work and Culture is very accessory to what I do, but I am asking the question for the both of them.


Solution

  • Page.UICulture is a wrapper around Thread.CurrentThread property and is ment for internal .NET framework use:

    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.

    Looking at the source code, you can clearly see that:

    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; }
    }