How make multi language site with masterpages in .net and c-sharp?


Back to learning
Created: 12/03/2011


How make multi language site with masterpages in .net (c#)?


You can see the NEW VERSION (2013) of this tutorial here: 


1) At the beginning i want to explain what we gonna do in this tutorial.

In this document we will create a short Visual Studio - Web Site project, and using one masterpage create 2 different sites, just for switch.  You will see how to make multi language site and that the switch between cultures endure along all your application using session. I used "Resources files" for diferent languages.

I used Visual Studio 2008.

You can download this project on the end of this tutorial.

2) Ok so lets start.
First create your application structure like this:




- In App_Code add simple clase with name "BseClass.cs" you will see what for it is later.

- In main root, create a masterpage with name you whant.

- Now add 2 diferent forms ".aspx" just for testing: for ex "index.aspx" and "site2.aspx" using your masterpage.

- Add Asp.Net folder "App_LocalResources".
In this folder:
For each form("index.aspx", "site2.aspx") create 1 resource file for each language, for ex: For index.aspx, create resource file "index.aspx.resx" <- this is our default resource file (en) wich will be used if there are no other languages selected, and add another one "index.aspx.es.resx" in this case for spanish language. Do it for "site2.aspx" to. So we have 2 languages for each sites.

3) Nex step is add some controles.

A) First add 1 dropdownlist in our MasterPage for language switch and create 2 items:
item 1 -> Value="en" for english you should use "en" not "english"
item 2 -> Value="es"
Code:


    <form id="form1" runat="server">
    <div>
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"  AutoPostBack="true"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="en" Text="English"></asp:ListItem>
        <asp:ListItem Value="es" Text="Spanish"></asp:ListItem>
        </asp:DropDownList>
    </div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>





B)
Second add some controls to your  "index.aspx" form:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table>
        <tr>
            <td colspan="2">
                <asp:Label ID="Label2" runat="server" meta:resourcekey="pagenumber" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label1" meta:resourcekey="Label1" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                <asp:Button ID="Button1" meta:resourcekey="Button1" runat="server"
                    Text="Button" onclick="Button1_Click" />
            </td>
        </tr>
    </table>
</asp:Content>





Now we need to expline how our controls will search for the resource files.
So in each control you need to add
meta:resourcekey="" and set between "" the name that  identificate your control in resource file in my case i set just some test names pagenumber and Label1

And in Resource file "index.aspx.resx": English



dont forget to indicate wich property of control you will use for ex:
pagenumbe.Text <- Text its the property that we whant to switch.

and in Resource file "index.aspx.es.resx": Spanish




How you can see we add one button just for go from one page to another one and show that in button text change to.

so add simple Event to the button:

onclick="Button1_Click" />

And in Code Behind:


    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("site2.aspx");
    }


REPEAT THIS STEP (B) FOR "SITE2.ASPX" ADDING CONTROLS TO .ASPX, ADDING ENGLISH AND SPANISH TRADUCTION IN RESOURCE FILES "SITE2.ASPX.RESX" AND "SITE2.ASPX.ES.RESX",  AND CODE BEHIND EVENT BUT REDIRECTING TO INDEX.ASPX

4) In this step we will add the code to our forms:
A) So start with the central page that is masterpage -> MasterPage.master.cs

When we swich the language we will set the dropdown selected item to the actual language!
This step its just fast way, so adjust it to your needs


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["myapplication.language"] != null)
            {
                if (Session["myapplication.language"] == "es")
                {
                    DropDownList1.SelectedIndex = 1;
                }
            }
        }
    }



We need to add an Event for our drowdownlist, wich will fire when we swich the language:

in same page add this code:


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["myapplication.language"] = DropDownList1.SelectedValue.ToString();
        Response.Redirect(Request.Url.ToString());
    }


This will set Session variable wich we called "myapplication.language", with value from selected item from our dropdownlist.
 
B) Next step, add code to BaseClass.cs
All our sites will inherit from this class. And this site will inherit from System.Web.UI.Page
Add this function to this class:


    protected override void InitializeCulture()
    {
        if (Session["myapplication.language"] != null)
        {
            string selectedLanguage = Session["myapplication.language"] as string;

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
        }
    }


This function reed our session variable value and set this value to the culture of our application: value can be like "en" or "es", etc.

C) And last step is add code to our "index.aspx" and "site2.aspx" forms:

Here just make that this 2 sites inherit from BaseClass.cs


public partial class
site2 : BaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {



Download the working project here:  /Data/Multilanguage.Net.rar


I hope this tutorial give you a good idea for make your Asp.Net site with multi language compatibility.
Thank you very much and let me your opinion :)