Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET

Developing an ASP.NET page with MasterPage and Localization

Rate me:
Please Sign up or sign in to vote.
4.57/5 (51 votes)
15 Nov 2014CPOL2 min read 237.3K   4K   97   73
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.

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:

C#
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:

ASP.NET
<%@ 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>[HelveticSolutions - 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:

C#
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:

C#
/// <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

  • 11th May 2007 - First version released.
  • 16th November 2014 - Namespace corrected

License

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


Written By
Architect Swissworx
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Swissworx
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

Comments and Discussions

 
GeneralExcellent Article Pin
M_Menon21-May-08 1:17
M_Menon21-May-08 1:17 
GeneralRe: Excellent Article Pin
Michael Ulmann15-Nov-14 11:52
Michael Ulmann15-Nov-14 11:52 
GeneralInitializing the language automatically Pin
Guillaume Hanique7-May-08 23:24
Guillaume Hanique7-May-08 23:24 
GeneralRe: Initializing the language automatically [modified] Pin
Vilim_Z10-Sep-09 9:17
Vilim_Z10-Sep-09 9:17 
GeneralHot Pin
paulklee5-May-08 17:11
paulklee5-May-08 17:11 
GeneralRe: Hot Pin
Michael Ulmann15-Nov-14 11:52
Michael Ulmann15-Nov-14 11:52 
GeneralPerfect example for localization Pin
fristi24-Mar-08 4:13
fristi24-Mar-08 4:13 
GeneralRe: Perfect example for localization Pin
Michael Ulmann15-Nov-14 11:52
Michael Ulmann15-Nov-14 11:52 
Thanks! Smile | :)
GeneralGreat, Nice One! Pin
theRexMundi12-Mar-08 2:21
theRexMundi12-Mar-08 2:21 
GeneralRe: Great, Nice One! Pin
Michael Ulmann15-Nov-14 11:53
Michael Ulmann15-Nov-14 11:53 
GeneralMultiple MasterPage or no MasterPage use the global.asax Pin
Jcmorin24-Jan-08 3:48
Jcmorin24-Jan-08 3:48 
GeneralExcellent Pin
merlin9819-Nov-07 4:00
professionalmerlin9819-Nov-07 4:00 
GeneralRe: Excellent Pin
Michael Ulmann15-Nov-14 11:53
Michael Ulmann15-Nov-14 11:53 
Generalgood solution for multilanguage website Pin
havan_nd7-Nov-07 17:57
havan_nd7-Nov-07 17:57 
GeneralRe: good solution for multilanguage website Pin
Michael Ulmann15-Nov-14 11:53
Michael Ulmann15-Nov-14 11:53 
GeneralQuestion about Code (possible bug) Pin
Urs Enzler14-Sep-07 2:04
Urs Enzler14-Sep-07 2:04 
GeneralRe: Question about Code (possible bug) Pin
Michael Ulmann8-Nov-07 7:29
Michael Ulmann8-Nov-07 7:29 
GeneralRe: Question about Code (possible bug) Pin
Urs Enzler8-Nov-07 20:17
Urs Enzler8-Nov-07 20:17 
GeneralPerfect Pin
xiangzi_camel17-Jun-07 21:17
xiangzi_camel17-Jun-07 21:17 
GeneralRe: Perfect Pin
Michael Ulmann15-Nov-14 11:53
Michael Ulmann15-Nov-14 11:53 
GeneralGood article. Pin
phucrobe16-May-07 22:36
phucrobe16-May-07 22:36 
GeneralRe: Good article. Pin
popota7-May-08 8:05
popota7-May-08 8:05 

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

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