citronas
citronas

Reputation: 19365

How to prevent Thread.CurrentThread.CurrentCulture to switch to the default browser value after setting it manually?

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:

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?

Upvotes: 1

Views: 4627

Answers (3)

Guffa
Guffa

Reputation: 700562

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

<globalization culture="de-DE"/>

Upvotes: 0

Ian Ringrose
Ian Ringrose

Reputation: 51927

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.

Upvotes: 1

Chris
Chris

Reputation: 28064

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.

Upvotes: 2

Related Questions