Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching language on ASP.NET app using master page

I am using Visual Studio 2008 and ASP.NET to build a web app. Most of my web pages are based on a single master page. This master page contains three button links that act as language switches; the 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 method SwitchLanguage:

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 to make the pages based on my master page exhibit a localised behaviour, i.e. get their resources from the appropriate local resx file according to culture. However, it is not working. They always appear in Spanish, which is my browser's default language. I have set some trace messages at the entry and exit points of SwitchLanguage, and apparently the current thread is not changing its culture info: every time SwitchLanguage is called, the current thread's culture is "es-ES", regardless of what my code has just set.

Is there any problem with my code or with the approach I am taking? Thanks.

like image 524
CesarGon Avatar asked Dec 12 '22 12:12

CesarGon


2 Answers

You could use OnAcquireRequestState in Global.asax to change the culture. It is called right after the session is loaded, but before any master page event.

So in the button event handler, you set a session variable to the desired current culture. Then you redirect to the current page. The Global.asax event can then pick up the new language, right before the masterpage loads:

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;
}
like image 153
Andomar Avatar answered Dec 30 '22 03:12

Andomar


For any body using @Andomar solution, the method should be named Application_AcquireRequestState positioned in Global.ascx code (e.g. below the Application_Start), and no event handlers are required to be attached. (If you do anything other than this you may encounter NullReferenceException even with no code in the method body! or your method will not run at all)
So the code will be 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;
}
like image 30
Mostafa Aghajani Avatar answered Dec 30 '22 05:12

Mostafa Aghajani