c# - how to create a class to initialize culture for localization -
i trying localize asp.net pages. far have been localize master page.
global.asax
protected void application_beginrequest(object sender, eventargs e) { httpcookie cookie = request.cookies["cultureinfo"]; if (cookie != null && cookie.value != null) { thread.currentthread.currentuiculture = new cultureinfo(cookie.value); thread.currentthread.currentculture = new cultureinfo(cookie.value); } else { thread.currentthread.currentuiculture = new cultureinfo("en-ca"); thread.currentthread.currentculture = new cultureinfo("en-ca"); } }
masterpage.master.cs
protected void page_load(object sender, eventargs e) { //only on non-postback because otherwise //the selected value not reach event handler correctly if (!page.ispostback) { ddllanguage.selectedvalue = thread.currentthread.currentculture.name; } } protected void ddllanguage_selectedindexchanged(object sender, eventargs e) { //sets cookie used global.asax httpcookie cookie = new httpcookie("cultureinfo"); cookie.value = ddllanguage.selectedvalue; response.cookies.add(cookie); //set culture , reload immediate effect. //future effects handled global.asax thread.currentthread.currentculture = new cultureinfo(ddllanguage.selectedvalue); thread.currentthread.currentuiculture = new cultureinfo(ddllanguage.selectedvalue); server.transfer(request.path); }
masterpage.master
<asp:dropdownlist id="ddllanguage" runat="server" onselectedindexchanged="ddllanguage_selectedindexchanged" autopostback="true"> <asp:listitem text="<%$ resources:resource, users_english %>" value="en-ca" /> <asp:listitem text="<%$ resources:resource, users_french %>" value="fr-ca" /> </asp:dropdownlist>
how can create common class other web pages inherit common class.
you can create class inherits system.web.ui.page (i.e. public class commonpage: system.web.ui.page) add/override methods want , pages can inherit newly created one.
Comments
Post a Comment