开发者

Switching language on ASP.NET app using master page

开发者 https://www.devze.com 2023-03-10 04:49 出处:网络
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 Cl

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 cult开发者_运维技巧ure 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.


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;
}


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;
}


override the InitializeCulture method on page level to set the UICulture

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号