Click here to Skip to main content
11,582,850 members (73,368 online)
Click here to Skip to main content

Developing an ASP.NET page with MasterPage and Localization

, 15 Nov 2014 CPOL 155.2K 3.3K 96
Rate this:
Please Sign up or sign in to vote.
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:

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>[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:

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. Smile | :)

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)

Share

About the Author

Michael Ulmann
Architect Helvetic Solutions
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Helvetic Solutions
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun16-Nov-14 18:02
memberHumayun Kabir Mamun16-Nov-14 18:02 
GeneralRe: My vote of 5 Pin
Michael Ulmann16-Nov-14 18:17
memberMichael Ulmann16-Nov-14 18:17 
QuestionNot working when using standard controls of asp.net in default page Pin
vipz0077-Aug-13 19:30
membervipz0077-Aug-13 19:30 
QuestionChange sitemap menu language from LocalResources or GlobalResources Pin
Member 310338317-Apr-13 12:15
memberMember 310338317-Apr-13 12:15 
Questionadding Url Rewrite or routing Pin
deching11-Sep-12 0:08
memberdeching11-Sep-12 0:08 
GeneralThanks Pin
richihu14-Aug-12 2:01
memberrichihu14-Aug-12 2:01 
QuestionWebsite is localized, but how to I put querystrings on them to show different URLs? Pin
Jason P. Weber28-Jan-12 19:36
memberJason P. Weber28-Jan-12 19:36 
QuestionThank you for providing such clear instructions and offering a downloadable source code Pin
Jason P. Weber23-Jan-12 7:13
memberJason P. Weber23-Jan-12 7:13 
QuestionPS - Beautiful implementation Pin
fitITOren6-Sep-11 4:30
memberfitITOren6-Sep-11 4:30 
AnswerRe: PS - Beautiful implementation Pin
Michael Ulmann15-Nov-14 11:48
memberMichael Ulmann15-Nov-14 11:48 
SuggestionWhy session and not cookies?? Pin
fitITOren6-Sep-11 4:29
memberfitITOren6-Sep-11 4:29 
GeneralRe: Why session and not cookies?? Pin
Michael Ulmann10-Oct-11 17:41
memberMichael Ulmann10-Oct-11 17:41 
QuestionSuper great - what about using cookies for remembering for next time...? Pin
fitITOren6-Sep-11 4:22
memberfitITOren6-Sep-11 4:22 
AnswerRe: Super great - what about using cookies for remembering for next time...? Pin
Michael Ulmann15-Nov-14 11:49
memberMichael Ulmann15-Nov-14 11:49 
Questionweb.config Pin
Suici24-Jun-11 3:47
memberSuici24-Jun-11 3:47 
Hi!

Thanks for sharing your knowledge.

You wrote "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."

Can you please tell me how can I code this?

Thank you!
AnswerRe: web.config Pin
Michael Ulmann11-Aug-11 17:19
memberMichael Ulmann11-Aug-11 17:19 
GeneralLocalization Page(have issues) Pin
Kleen201113-May-11 10:16
memberKleen201113-May-11 10:16 
GeneralRe: Localization Page(have issues) Pin
Michael Ulmann11-Aug-11 17:23
memberMichael Ulmann11-Aug-11 17:23 
GeneralMy vote of 5 Pin
Omar Gamil25-Dec-10 23:38
memberOmar Gamil25-Dec-10 23:38 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 11:50
memberMichael Ulmann15-Nov-14 11:50 
GeneralMy vote of 5 Pin
kimic4-Nov-10 13:59
memberkimic4-Nov-10 13:59 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 11:50
memberMichael Ulmann15-Nov-14 11:50 
GeneralMy vote of 5 Pin
gico13-Oct-10 21:09
membergico13-Oct-10 21:09 
GeneralRe: My vote of 5 Pin
Michael Ulmann15-Nov-14 11:50
memberMichael Ulmann15-Nov-14 11:50 
GeneralMy vote of 5 Pin
Keith Barrow21-Sep-10 8:00
memberKeith Barrow21-Sep-10 8:00 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 15 Nov 2014
Article Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid