C#.NET Localization with MasterPage

I recently started programming using C#.NET. The first thing I had to do with it, is make a complete website, kinda cool to learn about the language! At first it was easy, but then I had to localize the website for French and English. After hours of reading, I found a suitable solution written by Michael Ulmann.

  1. Open up your Website in Visual Studio
  2. Open up Global.asax and add these lines
    void Session_Start(object sender, EventArgs e)
    {
        /*
         * Set the default language of the website
         * In my case it is en-CA, but you could put anything
         */
        Session["MyCulture"] = "en-CA";
    }
  3. Create two links in your MasterPage.master
    <asp:linkbutton
        onclick="changeCulture_Click"
        commandargument="fr-CA"
        text="Français"
        runat="server"
        id="btnSetFR"/>
    <asp:linkbutton
        onclick="changeCulture_Click"
        commandargument="en-CA"
        text="English"
        runat="server"
        id="btnSetEN"/>
  4. Add the Click Event to your MasterPage.master.cs
        protected void changeCulture_Click(object sender,
           EventArgs e)
        {
            LinkButton senderLink = sender as
                LinkButton;
    
            Session["userCulture"] =
               senderLink.CommandArgument;
    
            Server.Transfer(Request.Path);
        }
  5. Create a new C# Class in App_Code, name it BasePage.cs and copy this code in it.
        public class MyPage : Page
        {
            protected override void InitializeCulture()
            {
                /*
                 * Get the culture from the Sessions
                 */
                string currentCulture =
                    Convert.ToString(Session["userCulture"]);
    
                /*
                 * Check if there's in a
                 * culture in the Sessions
                 * Otherwise, set the Default Culture
                 */
                if (!string.IsNullOrEmpty(currentCulture))
                    currentCulture = currentCulture;
                else
                    currentCulture = "en-CA"; 
    
                /*
                 * Apply the Culture to our thread.
                 */
                Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture(
                        currentCulture
                    );
    
                Thread.CurrentThread.CurrentUICulture =
                    new CultureInfo(currentCulture);
    
                base.InitializeCulture();
            }
        }
  6. Change the inheritance of all your web forms from Page to BasePage
  7. Now all you need to do is create your Resources in GlobalResources and LocalResources Folders

As I said, I learned this from an article posted on CodeProject by Michael Ulmann. If you would like to read the original article or download the source code, follow this link

Tags: , ,

This entry was posted on Sunday, August 31st, 2008 at 1:02 pm and is filed under ASP.NET, Developement, Globalization. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “C#.NET Localization with MasterPage”

Kiki August 17th, 2009 at 6:22 pm

Thanks, that’s what I needed!!!

Leave a Reply