views:

34

answers:

3

In my current application I want to implement ASP.Net localization with global resources. I have the problem, that after changing the CurrentThread.CurrentUICulture and CurrentThread.CurrentCulture and changing to another page, these values are overwritten by the browser default values.

I have a DropDownList that enables a selection between different languages. In the ItemChanged Event I store the culturename in the session, redirect to my defaultpage and use this code

protected override void InitializeCulture()
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
    base.InitializeCulture();
}

After switching to another contentpage, that does not override InitializeCulture I'm back to the default browser language. How can I make that persistent?

What options do I have? The following come to my mind:

  • On every *.aspx page I do implement InitializeCulture
  • I create a new class that derives from System.Web.UI.Page and overrides the InitializeCulture Eventhandler. Every *.aspx page I use derives from it.

Isn't there a more "built-in" way? ASP.net offers such good localization support, so I guess there must be an easier/more efficient way to achieve my goal. Which one is there?

+1  A: 

Have a look at Asp.Net modules, or hooking the events in global.asax.

Using a base class that all pages inhit from is another good option, but it is harder to reuse a base class between projects that a module.

Ian Ringrose
I assume you meant global.asax. Which event should I hook to? Session_Start or Application_Start won't do the job for this, because they are not executed every request
citronas
@citronas, Sorry it is a lot time since I have done this, from what I recall I used one of the per-request events in global.asax (it may have been a event on a standared module) - I don't have access to the source code of the system as it was for a old employer.
Ian Ringrose
I'll give you +1 anyway for proposing a HTTP Module.
citronas
+1  A: 

You need to re-set the culture in your base page's InitializeCulture method as you have described. This should be done upon each request. The CurrentCulture value is set based on the Accept-Languages header sent by the browser, and will always be set this way for each new request. There's no option but to set it manually afterwards for each new request, and Page.InitializeCulture is a good place to do it.

Chris
I decided for creating a new class that derives from System.Web.UI.Page and override InitializeCulture in it. Every contentpage derives from that class.
citronas
It's a good habit to always use a base page anyway. Then you can override the base methods like this one without having to touch every class file just to change what it inherits from.
Chris
A: 

You can put the culture in your web.config, in the system.web section:

<globalization culture="de-DE"/>
Guffa
I want to give the user the ability to change the culture while the app is running. Setting it manually in the web.config only helps me setting a desired default value but does not solve the problem
citronas
@citronas: I see... Well, as you are using content pages, the Page_Init event in the master page seems to be a good place to set the culture.
Guffa