4

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.

AceShot
  • 350
  • 3
  • 10
  • Can you please clarify what property you are talking about - link to MSDN would be the best... I.e. [System.Web.UI.Page.UICulture](https://msdn.microsoft.com/en-us/library/vstudio/system.web.ui.page.uiculture%28v=vs.100%29.aspx) marked as "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code."... (if you are talking about this one that MSDN have exact explanation too - "This property is a shortcut for the CurrentThread property") – Alexei Levenkov May 07 '15 at 17:05
  • I read that. But I do not understand the "is not intended to be used directly from your code". Also, CurrentUICulture is a CultureInfo object whereas UICulture is a simple String object. How come? Are they really interchangeable ? – AceShot May 07 '15 at 17:12
  • Note that UICulture will not work when doing async stuff that may execute on another thread. You need to pass such information to the methods that uses them. – sisve May 07 '15 at 19:19

2 Answers2

2

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; }
}
Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
0

They do exactly the same thing.

As you can see on the documentation page:

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

Shay
  • 183
  • 2
  • 13
  • Thanks. But CurrentUICulture and CurrentCulture are CultureInfo objects. Page.UICulture and Page.Culture are String objects. Are they still interchangeable regardless ? Am I not going to run into an error from using both in the same code ? – AceShot May 07 '15 at 17:17