Click here to Skip to main content
6,630,289 members and growing! (15,018 online)
Email Password   helpLost your password?
General Programming » Localisation » Localization     Intermediate License: The Code Project Open License (CPOL)

Developing an ASP.NET page with MasterPage and Localization

By Michael Ulmann

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.
C# 2.0, C# 3.0, Windows, .NET 2.0, .NET 3.0, ASP.NET, VS2005, Dev, Design
Posted:11 May 2007
Updated:7 May 2008
Views:49,362
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
33 votes for this article.
Popularity: 6.43 Rating: 4.23 out of 5
2 votes, 6.1%
1
2 votes, 6.1%
2
2 votes, 6.1%
3
5 votes, 15.2%
4
22 votes, 66.7%
5

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


Member
MCAD, MCPD Web Developer
Visit my Space: http://ukmichael.spaces.live.com
www.smart-soft.ch
Occupation: Software Developer (Senior)
Company: Smart-Soft
Location: Australia Australia

Other popular Localisation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralGreat work! Pinmemberjonrandahl3:20 15 Sep '09  
GeneralMerci vielmal ;-) PinmemberDarkjo22:24 12 Aug '09  
Questionerror in localization using masterpage Pinmembermdazeemuddin2:53 31 May '09  
AnswerRe: error in localization using masterpage PinmemberMichael Ulmann3:06 31 May '09  
GeneralRe: error in localization using masterpage Pinmembermdazeemuddin5:37 31 May '09  
GeneralRe: error in localization using masterpage PinmemberMichael Ulmann17:05 3 Jun '09  
GeneralTerrible Code! PinmemberTV Mogul6:44 26 Apr '09  
GeneralRe: Terrible Code! PinmemberMichael Ulmann15:41 26 Apr '09  
GeneralRe: Terrible Code! PinmemberTV Mogul1:30 27 Apr '09  
GeneralRe: Terrible Code! PinmemberTV Mogul1:49 27 Apr '09  
GeneralVery nice article PinmemberEzz Khayyat2:20 24 Dec '08  
GeneralRe: Very nice article PinmemberEzz Khayyat13:14 24 Dec '08  
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  
GeneralReally helpful but... Pinmemberfamous1:49 29 May '08  
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  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 May 2008
Editor: Smitha Vijayan
Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project