Switching language in an ASP.NET application using the home page

I am using Visual Studio 2008 and ASP.NET to build a web application. Most of my web pages are based on one main page. This main page contains three buttons that act as language switches; Click event handlers look like this:

 protected void lbuLangEN_Click(object sender, EventArgs e) { this.SwitchLanguage(string.Empty); } protected void lbuLangES_Click(object sender, EventArgs e) { this.SwitchLanguage("es"); } 

Then I have my private SwitchLanguage method:

 private void SwitchLanguage(string culture) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture); } 

As far as I understand, this should be enough so that pages based on my main page exhibit localized behavior, i.e. got their resources from the corresponding local resx file according to the culture. However, it does not work. They always appear in Spanish, this is my default browser. I set some trace messages at SwitchLanguage entry and exit points, and apparently the current thread does not change its culture information: every time SwitchLanguage is called, the current thread culture is "es-ES", regardless of what my code only what is installed.

Are there any problems with my code or with the approach I take? Thanks.

+4
source share
3 answers

You can use OnAcquireRequestState in Global.asax to change the culture. It is called immediately after loading the session, but before any event on the main page.

So, in the button event handler, you set the session variable to the desired current culture. Then you are redirected to the current page. The Global.asax event can pick up a new language immediately before loading the main page:

 protected void OnAcquireRequestState(object sender, EventArgs e) { string cultureName = "en-GB"; if (HttpContext.Current.Session != null && HttpContext.Current.Session["CultureName"] is string) cultureName = HttpContext.Current.Session["CultureName"]; if (Thread.CurrentThread.CurrentUICulture.Name == cultureName) return; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName); Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture; } 
+10
source

For any body using the @Andomar solution, the method should be named Application_AcquireRequestState, located in the Global.ascx code (for example, below Application_Start), and no event handlers should be attached. (If you do something other than this, you may encounter a NullReferenceException even without code in the body of the method! Or your method will not be run at all)
Thus, the code will look something like this:

 void Application_AcquireRequestState(object sender, EventArgs e) { string cultureName = "en-GB"; if (HttpContext.Current.Session != null && HttpContext.Current.Session["CultureName"] is string) cultureName = HttpContext.Current.Session["CultureName"]; if (Thread.CurrentThread.CurrentUICulture.Name == cultureName) return; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName); Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture; } 
+2
source

override page-level InitializeCulture method to set UICulture

0
source

All Articles