Click here to Skip to main content
Click here to Skip to main content

Developing an ASP.NET page with MasterPage and Localization

, 7 May 2008 CPOL
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.


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.


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" 

<html xmlns="" >
<head runat="server">
    <title>[Smart-Soft - Masterpage with Localization Support]</title>

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

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

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:

/// <span class="code-SummaryComment"><summary></span>
/// Custom base page used for all web forms.
/// <span class="code-SummaryComment"></summary></span>
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

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.


  • 11 May 2007 - First version released.


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
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5
My company:
Hopp Schwiiz Smile | :)

Comments and Discussions

QuestionNot working when using standard controls of in default page Pinmembervipz0077-Aug-13 19:30 
QuestionChange sitemap menu language from LocalResources or GlobalResources PinmemberMember 310338317-Apr-13 12:15 
Questionadding Url Rewrite or routing Pinmemberdeching11-Sep-12 0:08 
GeneralThanks Pinmemberrichihu14-Aug-12 2:01 
QuestionWebsite is localized, but how to I put querystrings on them to show different URLs? PinmemberJason P. Weber28-Jan-12 19:36 
QuestionThank you for providing such clear instructions and offering a downloadable source code PinmemberJason P. Weber23-Jan-12 7:13 
QuestionPS - Beautiful implementation PinmemberfitITOren6-Sep-11 4:30 
SuggestionWhy session and not cookies?? PinmemberfitITOren6-Sep-11 4:29 
GeneralRe: Why session and not cookies?? PinmemberMichael Ulmann10-Oct-11 17:41 
QuestionSuper great - what about using cookies for remembering for next time...? PinmemberfitITOren6-Sep-11 4:22 
Questionweb.config PinmemberSuici24-Jun-11 3:47 
AnswerRe: web.config PinmemberMichael Ulmann11-Aug-11 17:19 
GeneralLocalization Page(have issues) PinmemberKleen201113-May-11 10:16 
GeneralRe: Localization Page(have issues) PinmemberMichael Ulmann11-Aug-11 17:23 
GeneralMy vote of 5 PinmemberOmar Gamil25-Dec-10 23:38 
GeneralMy vote of 5 Pinmemberkimic4-Nov-10 13:59 
GeneralMy vote of 5 Pinmembergico13-Oct-10 21:09 
GeneralMy vote of 5 PinmemberKeith Barrow21-Sep-10 8:00 
GeneralGreat work! Pinmemberjonrandahl15-Sep-09 2:20 
GeneralMerci vielmal ;-) PinmemberDarkjo12-Aug-09 21:24 
GeneralRe: Merci vielmal ;-) PinmemberMichael Ulmann11-Aug-11 17:22 
Questionerror in localization using masterpage Pinmembermdazeemuddin31-May-09 1:53 
AnswerRe: error in localization using masterpage PinmemberMichael Ulmann31-May-09 2:06 
I don't fully understand your issue.
You mentioned that all your pages derive from the standard .NET System.Web.UI.Page class except the OrderOnline page wich derives from the BasePage, right? This results in not having the users selected culture applied in any page but the OrderOnline page. But I assume that the idea would be that all pages support localization and hence, they should all derive from the BasePage class.
What do you exactly mean by you can translate? Are we talking about adding/changing resources or runtime behaviour by clicking one of the language selection buttons e.g. page localization?
Would be good if you could give me a bit more information or even a small demo solution where I can have a look at to determine the problem. You can send this to if you like.
GeneralRe: error in localization using masterpage Pinmembermdazeemuddin31-May-09 4:37 
GeneralRe: error in localization using masterpage PinmemberMichael Ulmann3-Jun-09 16:05 
GeneralTerrible Code! PinmemberTV Mogul26-Apr-09 5:44 
GeneralRe: Terrible Code! PinmemberMichael Ulmann26-Apr-09 14:41 
GeneralRe: Terrible Code! PinmemberTV Mogul27-Apr-09 0:30 
GeneralRe: Terrible Code! PinmemberTV Mogul27-Apr-09 0:49 
GeneralVery nice article PinmemberEzz Khayyat24-Dec-08 1:20 
GeneralRe: Very nice article PinmemberEzz Khayyat24-Dec-08 12:14 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:34 
Generalgreat article PinmemberSimon Griffiths3-Dec-08 1:49 
Generalresource tags in master page Pinmemberdaniel.zolnjan23-Oct-08 8:26 
GeneralThanks but I have a problem... Pinmemberswissroll5521-Oct-08 4:54 
GeneralRe: Thanks but I have a problem... Pinmemberswissroll5521-Oct-08 4:58 
GeneralAwesome PinmemberSuresh2712928-Aug-08 5:52 
GeneralGreat! PinmemberShevchenko725-Aug-08 12:44 
GeneralReally helpful but... Pinmemberfamous29-May-08 0:49 
GeneralRe: Really helpful but... PinmemberKeith Barrow29-Sep-10 5:09 
GeneralExcellent Article PinmemberM_Menon21-May-08 1:17 
GeneralInitializing the language automatically PinmemberGuillaume Hanique7-May-08 23:24 
GeneralRe: Initializing the language automatically [modified] PinmemberMember 224200410-Sep-09 9:17 
GeneralHot Pinmemberpaulklee5-May-08 17:11 
GeneralPerfect example for localization Pinmemberfristi24-Mar-08 4:13 
GeneralGreat, Nice One! PinmembertheRexMundi12-Mar-08 2:21 
GeneralMultiple MasterPage or no MasterPage use the global.asax PinmemberJcmorin24-Jan-08 3:48 
GeneralExcellent Pinmembermerlin9819-Nov-07 4:00 
Generalgood solution for multilanguage website Pinmemberhavan_nd7-Nov-07 17:57 
GeneralQuestion about Code (possible bug) PinmemberUrs Enzler14-Sep-07 2:04 

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 | Mobile
Web02 | 2.8.141022.1 | Last Updated 7 May 2008
Article Copyright 2007 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid