Propagting the resource culture change in master page to the content web pages

感情迁移 提交于 2019-12-11 11:05:29

问题


I'm a noob when it comes to web applications. But i'm trying my best to learn it using ASP.NET 2.0 and sorry for the long post.

I have a master page(M1) and 3 different content pagesC1,C2,C3 which basically use the master page M1 for filling its respective contents in the content placeholder.

All the web-forms are localized and appropriate language resource strings are added in the resource (xml) files ex: Resource.en-US.xml,Resource.de-DE.xml and so on.Finally the resources are referred in the code after setting up the appropriate current culture and current uiculture.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
//where the btnSubmit is a control on the form
btnSubmit.Text = rm.GetString("Submit", Thread.CurrentThread.CurrentCulture);

Now comes the question, I have included an option of changing the display language in the master page available to the user as an asp:linkbutton with an asp:image. Whenever the user clicks the linkbutton for the desired language, the content page controls shall display the whole content strings corresponding to the culture selected.

  1. How do i achieve this ?

  2. Do i have to implement Session variables to include the selected language ? Or storing in Cookie would also do the job ?

What i tried

On master page load event. I tried calling a method SetCultureSpecificInformation, which basically sets the culture and uiculture properties of CurrentThread and store the selected language inside a session variable.

Also a similar implementation on the asp:linkbutton OnClick eventhandler. In this case it modifies the session variable.

Finally refer the session variable on the content web page OnPage_Load event.

But somehow the above approach is not yielding desired results. The switching of language is not consistent. Anyone out there who can help me out with a good enough design approach for implementing the same.

Thanks in advance


回答1:


Add Global.asax file: write this piece of code

 void Application_BeginRequest(Object sender, EventArgs e)
    {
        // Code that runs on application startup
        HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"];
        if (cookie != null && cookie.Value != null)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
        }
        else
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
        }
    }

And on Masterpage page

protected void ddlanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["language"] = ddlanguage.SelectedValue;

        //Sets the cookie that is to be used by Global.asax
        HttpCookie cookie = new HttpCookie("CultureInfo");
        cookie.Value = ddlanguage.SelectedValue;
        Response.Cookies.Add(cookie);

        //Set the culture and reload for immediate effect.
        //Future effects are handled by Global.asax
        Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlanguage.SelectedValue);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlanguage.SelectedValue);

        if (cookie.Value == "en")
        {
            Session["ddindex"] = 0;
        }

        else if (cookie.Value == "fr")
        {
           Session["ddindex"] = 1;
        }

        else if (cookie.Value == "de")
        {
           Session["ddindex"] = 2;
        }
        Server.Transfer(Request.Path);
    }
}

Blog article: create multiple language website in asp.net c#




回答2:


In my case used a couple of buttons for setting culture from the master page, then used this code in the master page's code behid:

 protected void IdiomButton_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton theButton = (ImageButton)sender;
        Session["culture"] = theButton.ID == "ItalianButton" ? CultureInfo.CreateSpecificCulture("it-IT") : CultureInfo.CreateSpecificCulture("en-US");
        Response.Redirect(Request.RawUrl);
    }

Then in every child page I used :

protected override void InitializeCulture()
    {
        if (Session["culture"] != null)
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Session["culture"].ToString());
        else
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("it-IT");
    }


来源:https://stackoverflow.com/questions/11201029/propagting-the-resource-culture-change-in-master-page-to-the-content-web-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!