How make multi language site with masterpages in .net using Global.asax file - Version 2 (2013)

Created: 18/06/2013 12:14:32 p.m.

How make multi language site with masterpages in .net using Global.asax file
And implement language selection by DropDownList
Version 2 (2013)

First maby it will be better that you see this previous tutorial: Link

Here we gona see how to make multi language web site (In this case Spanish-English) using Master Pages and Global.asax file.
You can see how work with MasterPages in this: Link

1) First create your structure like this: 

2) So basically we have 1 master page, and 2 pages that use the master, and we have just simple Global.asax file.

3) Next step Open the MasterPage.master and add some labels like i have done in my project:

    <form id="form1" runat="server">
        <!-- Main Menu -->
        <a href="Default.aspx">
            <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Go To Default"></asp:Label>
        <a href="Contact.aspx">
            <asp:Label ID="Label2" meta:resourcekey="menuItemContact" runat="server" Text="Go To Contact"></asp:Label>

This 2 labeles will be my main menu in which i will change the text according to the language i selected

to this 2 labels add the resourcekey properties that we will add later to the resource files to change the text according to the selected language

4) Add the DropDownList were i will select my language to switch, and add the SelectedIndexChange event, dont forget to set AutoPostBack to true.

    <br />
    <asp:DropDownList ID="DropDownList_Language" runat="server" Height="20px" Width="170px" 
        onselectedindexchanged="DropDownList_Language_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem Value="es-AR">Spanish</asp:ListItem>
        <asp:ListItem Value="en-US">English</asp:ListItem>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">


5) Now, add some code to MasterPage.master.cs

Here, basically, on page load we set the current language to the DropDown.
On language change we set the Session variable "myapplication.language" to the current selected culture that i will use in Global.asax

So: Set current language and set Session language variable with the new one.

    protected void Page_Load(object sender, EventArgs e)
        if(Session["myapplication.language"] != null && !IsPostBack)
            DropDownList_Language.Items.FindByValue(Session["myapplication.language"].ToString()).Selected = true;
    protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e)
            case "en-US": this.SetMyNewCulture("en-US");
            case "es-AR": this.SetMyNewCulture("es-AR");

    private void SetMyNewCulture(string culture)
        Session["myapplication.language"] = culture;


6) Now, go to the Global.asax file.
Include the 2 required namespace

<%@ Application Language="C#" %>

<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Threading" %>

<script runat="server">

and add the fallowing method:

    void Application_AcquireRequestState(object sender, EventArgs e)
        HttpContext context = HttpContext.Current;

        if (context.Session["myapplication.language"] != null)
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(context.Session["myapplication.language"].ToString().Trim());
            Thread.CurrentThread.CurrentCulture = new CultureInfo(context.Session["myapplication.language"].ToString().Trim());

This method will retrieve the current selected language from the session that you have assigned with DropDown event and will set it to the current Thread culture of your Web Site.


7) Finally add the resource files which contains the text of each language:

In my case i maked multi language menu (To swich between Spanish and English)

Now add some text to contact.aspx files and a simple label to swich text in contact page

8) Contact Page:

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" meta:resourcekey="textLanguage" Text=""></asp:Label>

Selected Spanish

Selected English

You can modify the project to adapt to your needs,
Download the full project here: Download Source


Let your comments! :)


User: Anders Comment date: 04/11/2013 07:12:03 p.m.
Thanks for article.

What schould i write in code behind c#.

Label1.Text = ????

User: maksim Comment date: 05/11/2013 07:35:49 p.m.
Hey Anders you dont need to put anything in label1.Text, because it will automatically take text from the resource file.
User: pradheeban Comment date: 22/11/2013 05:22:58 a.m.
Nice but i not like that... This is more complicated...
User: Maksim Comment date: 23/11/2013 09:04:38 p.m.
Hey pradeeban thank you for comment, what solution will you applied instead of this method?

User: John Comment date: 01/02/2014 12:25:50 a.m.
Thanks ! You are a life saver !

Go to Comments