Go to Comments





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


Created: 12/03/2011 09:05:58 p.m.






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:  http://www.docstorus.com/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 :)






Comments:

User: vietvoquoc Comment date: 13/01/2012 10:09:53 p.m.
Thanks!Very useful for me.
User: Qubitek Comment date: 22/01/2012 02:41:49 a.m.
Thank you for your positive comment!
User: mak Comment date: 24/01/2012 01:04:28 p.m.
Message directed to the guy who sended this:
-------------
when am executing this code am getting Error Class1.InitializeCulture(): no suitable method found to override . please let me know the proper solution for this error with clear answer or code.
thanks in advance.
-------------
Can you resend me your email?
 this site had an error and your email doesent sended to me!
Thnx
User: hahuu Comment date: 23/04/2012 01:21:53 a.m.
Mình download dự án về hok dc. Bạn giúp mình với. huhuhu....
Cảm Ơn nhiều!!
User: Maksim Comment date: 23/04/2012 10:14:23 p.m.
Hello Hahuu, i dont understand what you realy mean, but if you meen that link was broken, i have updated it to my server, just downloat it again, thank you for comment!

http://www.docstorus.com/Data/Multilanguage.Net.rar
User: Jorge Comment date: 06/08/2012 08:16:48 a.m.
Any ideas on how to do it through a bulletedlist but not through a dropdown? Thanks in advance.
User: Greg H. Comment date: 14/02/2013 09:11:58 p.m.
Satellite assemblies is a good approach.
Some simple tool to crate satellite assemblies:
www.tbs-apps.com/lsacreator
User: maksim Comment date: 18/02/2013 09:36:14 a.m.
gh3303@gmail.com...
Wtf?
User: mimirus Comment date: 21/02/2013 05:27:54 p.m.
super, super.... after 2 days looking, thanks
User: maksim Comment date: 22/02/2013 01:35:11 p.m.
Thank you mimirus for your visit! :)
User: vikas Comment date: 05/12/2013 04:04:26 a.m.

Hello Dear,

So nice work. really you great...

User: maksim Comment date: 05/12/2013 07:49:47 a.m.
Thank you very much for your support vikas !