1

So far, I was under the impression that ASP.NET web forms localization works by overriding Page.InitializeCulture and setting Page.Culture and/or Page.UICulture according to some user choice. In fact, the documentation of Page.InitializeCulture even says so:

The InitializeCulture method contains no coding logic. Control developers extending the functionality of the Page class can override the InitializeCulture method to initialize the Culture and UICulture information for the page.

However, following the link to Culture or UICulture yields the following warning:

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

Is the warning rubbish or am I on the wrong track and localization should be done in some other way?

Heinzi
  • 167,459
  • 57
  • 363
  • 519

1 Answers1

1

The intended way is, I believe:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

The Page.UICulture entry on MSDN later says:

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

I would say the warning is mostly nonsense, as it just shortcuts to the thread method of doing it.

Tho I wouldn't, personally, set the culture on the Page, I'd do it somewhere in the global.ascx. E.g. Application_BeginRequest or Application_Start.
That way the culture is set before the Page Lifecycle has kicked in.

Potentially, that is the direction the warning is pointing you towards.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
  • Makes sense. From a stylistic point of view, I'd rather use the Page properties than the Thread properties, since [a single aspx request can change the thread during the lifecycle](http://stackoverflow.com/a/657748/87698), and I actually want to set the culture of the *current request* rather than the one of the *thread* (which is an implementation detail of the ASP.NET execution engine). – Heinzi Feb 04 '16 at 16:03