views:

118

answers:

1

I'm working on a asp.net web application and one of the requirements is that the user has to be able to select the language they want. I"m using Resx files to store the locals. What my question is do I need to change the CurrentCulture of the thread every time a page is loaded or is there a way to have it handled automatically when a logged in user moves from one page to the next.

+1  A: 

Yes, I believe you need to set it every time. To make it even worse, you have to do it by overriding the InitializeCulture method of the Page class. I created a SitePage that all pages in my project inherit from instead of Page to do this.

public class SitePage : Page
{
 protected override void InitializeCulture()
 {
  base.InitializeCulture();

  // Set both the CurrentCulture (for currency, date, etc) conversion, and the CurrentUICulture for resource file lookup.
  Thread.CurrentThread.CurrentCulture = whatever;
  Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
 }

}

Further reading: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Greg
What about doing it at Application_BeginRequest in the Global.asax, would that work also since it gets called before every thing else get called
Superdumbell
I just opened up the Page class in reflector and the InitializeCulture is just a virtual method so it just gives you a place to setup the culture so doing it in Application_BeginRequest should work. If it does not I will post otherwise
Superdumbell
I know there was some reason that *I* couldn't do use Application_BeginRequest, but I can't remember if it was a global restriction.
Greg