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
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
Let's start with the Global.asax file:
void Session_Start(object sender, EventArgs e)
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
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="http://www.w3.org/1999/xhtml" >
<title>[Smart-Soft - Masterpage with Localization Support]</title>
<form id="theForm" runat="server">
<asp:contentplaceholder id="ContentPlaceHolder" runat="server">
<asp:LinkButton ID="btnSetGerman" runat="server" Text="Deutsch"
<asp:LinkButton ID="btnSetEnglish" runat="server" Text="English"
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;
Session["MyCulture"] = senderLink.CommandArgument;
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
public class BasePage : Page
private const string m_DefaultCulture = "en-GB";
protected override void InitializeCulture()
string culture = Convert.ToString(Session["MyCulture"]);
if (!string.IsNullOrEmpty(culture)) Culture = culture;
else Culture = m_DefaultCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
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.
- 11 May 2007 - First version released.