Click here to Skip to main content
Licence CPOL
First Posted 11 May 2007
Views 97,382
Downloads 929
Bookmarked 75 times

Developing an ASP.NET page with MasterPage and Localization

By Michael Ulmann | 7 May 2008
The MasterPage is derived from UserControl and thus, does not support the method 'InitializeCulture()'; a bit more coding is required in order to make the ASP.NET MasterPage localizable.
2 votes, 5.1%
1
2 votes, 5.1%
2
2 votes, 5.1%
3
6 votes, 15.4%
4
27 votes, 69.2%
5
4.61/5 - 39 votes
4 removed
μ 4.28, σa 2.01 [?]

Introduction

While seeking on the internet for a solution to implement localization within an ASP.NET application using a MasterPage, I realized that a lot of people have got the same problem to solve. Unfortunately, I could not find a suitable solution thus, I intended to do my own implementation.

Background

The solution presented within this article uses the standard localization mechanism of the .NET framework.

Using the code

The published solution uses the Session object as storage for the currently selected culture. This will be initialized during the Session_Start method that is part of the global.asax file.

If a culture change is requested by the user, the MasterPage changes the stored culture in the Session object.

In a BasePage that inherits from Page, the method InitializeCulture is overridden and sets the appropriate culture information stored in the Session object to the current thread. Therefore, every Web Form needs to derive from this BasePage.

Let's start with the Global.asax file:

void Session_Start(object sender, EventArgs e) 
{
    //set english as default startup language
    Session["MyCulture"] = "en-GB";
}

Alternatively, the culture can be defined in the Web.config file with the key <globalization culture="en-GB" /> and then be processed and stored in the Session object from the Session_Start method.

The next step is the master page:

<%@ Master Language="C#" AutoEventWireup="true" 
           CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>[Smart-Soft - Masterpage with Localization Support]</title>
</head>

<body>
    <form id="theForm" runat="server">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder" runat="server">
        </asp:contentplaceholder>
    </div>
    <div style="margin-top:20px;">
        <asp:LinkButton ID="btnSetGerman" runat="server" Text="Deutsch" 
           CommandArgument="de-CH" OnClick="RequestLanguageChange_Click">
        </asp:LinkButton>  
        <asp:LinkButton ID="btnSetEnglish" runat="server" Text="English" 
           CommandArgument="en-GB" OnClick="RequestLanguageChange_Click">
        </asp:LinkButton>
    </div>
    </form>
</body>
</html>

The buttons to change the culture can be either placed in the MasterPage directly, or in any embedded UserControl. In order to determine the requested language, the CommandArgument attribute of the LinkButton is used.

..And the code-behind of the master page:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void RequestLanguageChange_Click(object sender, EventArgs e)
    {
        LinkButton senderLink = sender as LinkButton;

        //store requested language as new culture in the session
        Session["MyCulture"] = senderLink.CommandArgument;

        //reload last requested page with new culture
        Server.Transfer(Request.Path);
    }
}

The requested language, passed within the CommandArgument, is processed and stored in the Session object. Afterwards, the initially requested page will be reloaded on the server side.

Last but not least, the BasePage:

/// <summary>
/// Custom base page used for all web forms.
/// </summary>
public class BasePage : Page
{
    private const string m_DefaultCulture = "en-GB";
    
    protected override void InitializeCulture()
    {
        //retrieve culture information from session
        string culture = Convert.ToString(Session["MyCulture"]);

        //check whether a culture is stored in the session
        if (!string.IsNullOrEmpty(culture)) Culture = culture;
        else Culture = m_DefaultCulture;

        //set culture to current thread
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

        //call base class
        base.InitializeCulture();
    }
}

As mentioned above, the InitializeCulture method is overridden, and gets the stored culture from the Session object and assigns it to the currently running thread.

Remark: In this article, only the culture was mentioned. Of course, there is also the UI culture. But it is not of any further interest in this article since the handling is absolutely identical. For more information, please see the MSDN pages. :)

For a running example, download the Zip file above.

History

  • 11 May 2007 - First version released.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michael Ulmann

Architect
Helvetic Solutions
Australia Australia

Member
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5
My company: www.helveticsolutions.com
Hopp Schwiiz Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko22:34 17 Dec '08  
Generalgreat article PinmemberSimon Griffiths2:49 3 Dec '08  
Generalresource tags in master page Pinmemberdaniel.zolnjan9:26 23 Oct '08  
GeneralThanks but I have a problem... Pinmemberswissroll555:54 21 Oct '08  
GeneralRe: Thanks but I have a problem... Pinmemberswissroll555:58 21 Oct '08  
GeneralAwesome PinmemberSuresh271296:52 28 Aug '08  
GeneralGreat! PinmemberShevchenko713:44 25 Aug '08  
Great article, very helpful Smile | :) Thanks!
GeneralReally helpful but... Pinmemberfamous1:49 29 May '08  
GeneralRe: Really helpful but... PinmemberKeith Barrow6:09 29 Sep '10  
GeneralExcellent Article PinmemberM_Menon2:17 21 May '08  
GeneralInitializing the language automatically PinmemberGuillaume Hanique0:24 8 May '08  
GeneralRe: Initializing the language automatically [modified] PinmemberMember 224200410:17 10 Sep '09  
GeneralHot Pinmemberpaulklee18:11 5 May '08  
GeneralPerfect example for localization Pinmemberfristi5:13 24 Mar '08  
GeneralGreat, Nice One! PinmembertheRexMundi3:21 12 Mar '08  
GeneralMultiple MasterPage or no MasterPage use the global.asax PinmemberJcmorin4:48 24 Jan '08  
GeneralExcellent Pinmembermerlin9815:00 9 Nov '07  
Generalgood solution for multilanguage website Pinmemberhavan_nd18:57 7 Nov '07  
GeneralQuestion about Code (possible bug) PinmemberUrs Enzler3:04 14 Sep '07  
GeneralRe: Question about Code (possible bug) Pinmemberuliderknecht8:29 8 Nov '07  
GeneralRe: Question about Code (possible bug) PinmemberUrs Enzler21:17 8 Nov '07  
GeneralPerfect Pinmemberxiangzi_camel22:17 17 Jun '07  
GeneralGood article. Pinmemberphucrobe23:36 16 May '07  
GeneralRe: Good article. Pinmemberroundass9:05 7 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 7 May 2008
Article Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid