Applying localization From MasterPages

by mosessaur| 18 February 2008| 15 Comments

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">
   2:     <div>
   3:         <asp:DropDownList ID="cmbCulture" runat="server" AutoPostBack="True" 
   4:             OnSelectedIndexChanged="cmbCulture_SelectedIndexChanged">
   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>
   8:         </asp:DropDownList>        
   9:         <asp:ContentPlaceHolder ID="cph" runat="server"/>
  10:     </div>
  11: </form>

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()
   2: {
   3:     HttpCookie cultureCookie = Request.Cookies["Culture"];
   4:     string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
   5:     if (!string.IsNullOrEmpty(cultureCode))
   6:     {
   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;
  12:     }
  13:     base.InitializeCulture();
  14: }

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)
   2: {
   3:     //Save Current Culture in Cookie- will be used in InitializeCulture in BasePage
   4:     Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.SelectedValue));
   5:     //Reload
   6:     Response.Redirect(Request.Url.AbsolutePath);
   7: }

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)
   2: {
   3:     if (!Page.IsPostBack)
   4:     {
   5:         HttpCookie cultureCookie = Request.Cookies["Culture"];
   6:         string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
   7:         if (!string.IsNullOrEmpty(cultureCode))
   8:         {
   9:             cmbCulture.SelectedValue = cultureCode;
  10:         }
  11:     }
  12: }

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)

kick it on DotNetKicks.com

Comments

Davis Sousa
Davis Sousa Poland on 3/7/2008 6:00 PM Man, you saved my day! =D

Thank you!
mosessaur
mosessaur Egypt on 3/7/2008 9:29 PM You are welcome Davis, good to know it helped!
Aleksandra
Aleksandra United States on 4/6/2008 2:35 PM Thank you!
Ravishankar Visvan
Ravishankar Visvan India on 5/4/2008 12:26 AM Nice article...helped me a lot
mosessaur
mosessaur Egypt on 5/4/2008 2:20 PM Thank you, it is great to see that helped you all
amr embaby
amr embaby Saudi Arabia on 7/5/2008 1:32 AM yea , i thinks it is ok , thnks mosa
marc
marc United States on 7/14/2008 6:05 PM Yeah, very slick and straight forward.  Thanks.
Brice
Brice Costa Rica on 8/24/2008 5:34 PM Definitely works.  The sample is completely stripped down to see every detail of what is necessary and nothing more.  Well done.
mosessaur
mosessaur Egypt on 8/24/2008 5:37 PM @ Brice Thank you for letting me that it was useful and clear. I really appreciate that.
Ahmed Khalifa
Ahmed Khalifa Egypt on 11/17/2008 3:28 AM Your code is great.
It works.
تمام يامان
conspic
conspic United Kingdom on 11/17/2008 10:52 AM Just tried your method of implementing this and it works and is far cleaner than other methods I've used. Only thing is that the culture is not applied to any user controls loaded in the page. Any ideas?
conspic
conspic United Kingdom on 11/17/2008 10:56 AM Doh! Forget the above comment - it was my own stupid mistake Smile
Umm Hamad
Umm Hamad Qatar on 2/27/2009 12:52 PM جزاك الله خيراً
Truly an elegant solution!
Vijay Jadhav
Vijay Jadhav India on 7/1/2009 10:42 PM Yep!

You have saved my lot of Headache.

Really nice article.  

Your code is great.

Thanks.
Vijay Jadhav
Vijay Jadhav India on 7/6/2009 4:05 AM Hi Guys,

We can also apply Theme from Master page to all Content pages using the same

concept. Well done mosessaur.

Nice work.

Comments are closed