Moses' Blog

Living {.net} lifestyle

Muhammad Mosa

Moses' profile picture
Logo
Software Engineer.
MCT, MCSD.NET,
MCTS: .Net 2.0 Web, Windows, Distributed Applications
MCTS: .Net 3.5 WF Application Development
MCTS: WSS 3.0, MOSS 2007 Configuration & App Dev
MCPD: Enterprise Application Developer

Send mail My Live Space Moses on Facebook Twitter Moses on Technorati

Sponsors



Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent Comments

Comment RSS

Community Credit

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Applying localization From MasterPages

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

Posted: Feb 18 2008, 22:06 by mosessaur | Comments (12) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: ASP.NET | Tips
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Comments

Add comment


(For Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading