The scenario here is I want to apply localization to my web application and place language selection on MasterPage. I have made my own solution and applied this with assistance of cookies. View demo.
I'll demonstrate that in my sample. The sample has a MasterPage bellow is a snippet from its ASPX code:
1: <form id="form1" runat="server">
3: <asp:DropDownList ID="cmbCulture" runat="server" AutoPostBack="True"
5: <asp:ListItem Value="en-US">English</asp:ListItem>
6: <asp:ListItem Value="ar-EG">Arabic</asp:ListItem>
7: <asp:ListItem Value="de-DE">German</asp:ListItem>
9: <asp:ContentPlaceHolder ID="cph" runat="server"/>
Simply when the user selects his language from the DropDownList the page post backs and change the language.
I made a BasePage class that inherits from System.Web.UI.Page. And wrapped the basic implementation of InitializeCulture on it as the following:
1: protected override void InitializeCulture()
3: HttpCookie cultureCookie = Request.Cookies["Culture"];
4: string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
5: if (!string.IsNullOrEmpty(cultureCode))
7: this.UICulture = cultureCode;
8: this.Culture = cultureCode;
9: CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureCode);
10: System.Threading.Thread.CurrentThread.CurrentCulture = culture;
11: System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
As you might notice, I'm using a cookie that stores/holds the culture code that the user selected from the DropDownPage in the MasterPage.
It worth to mention that InitializeCulture method executes very early in page life cycle. So for sure it will run before the event handler of the DropDownList SelectedIndexChanged. I depend on the SelectedIndexChanged event of the DropDownList to get the selected culture and store it on a cookie. But that means the culture will not be applied from the first time. User must refresh the page or move to another be able to see his language applied.
I worked around this issue by redirecting the user again to the same page he browse on the SelectedIndexChanged event of the DropDownList in MasterPage:
1: protected void cmbCulture_SelectedIndexChanged(object sender, EventArgs e)
3: //Save Current Culture in Cookie- will be used in InitializeCulture in BasePage
4: Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.SelectedValue));
Still have one issue, I need to set the selected language on the DropDownList. So when user moves from page to page he sill can observe his selected language on the DropDownList so he can change it if he wishs. So to do this I had to handle Page Load event of the MasterPage as the following:
1: protected void Page_Load(object sender, EventArgs e)
3: if (!Page.IsPostBack)
5: HttpCookie cultureCookie = Request.Cookies["Culture"];
6: string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
7: if (!string.IsNullOrEmpty(cultureCode))
9: cmbCulture.SelectedValue = cultureCode;
Of course same target can be achieved without using cookie or redirecting. For example you need to use Request.Form["DropDownListClientID"] to get your culture information. To do this you should know the ClientID of the DropDownList. And as you know it is within the MasterPage. So some would get it and hard coded it in the InitializeCulture method, and other would love to get programaticly somehow.
Download the code (14.25 Kb)