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


Back to learning
Created: 18/06/2013

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:

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


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.

    </div>
    <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:DropDownList>
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
           
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Result:


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.ClearSelection();
            DropDownList_Language.Items.FindByValue(Session["myapplication.language"].ToString()).Selected = true;
        }
    }
    protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch(DropDownList_Language.SelectedValue)
        {
            case "en-US": this.SetMyNewCulture("en-US");
                break;
            case "es-AR": this.SetMyNewCulture("es-AR");
                break;
            default:
                break;
        }
        Response.Redirect(Request.Path);
    }

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


Result:


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.

Result:


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>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" meta:resourcekey="textLanguage" Text=""></asp:Label>
</asp:Content>

Result:
Selected Spanish


Selected English




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

Property:

Let your comments! :)