Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET
Tip/Trick

Create Multi Language Web-Site in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.30/5 (7 votes)
28 Mar 2012CPOL1 min read 57.7K   13   6
Create a multi-language website in ASP.NET.

Step 1: Start a new web site with C# and give it the Name Multilanguage website. It will give you a Default.aspx and Default.aspx.cs files.

Step 2: Now Design the form by putting three labels and three buttons on the form; set the id for the label as well as the button.

Step 3: Now go to Solution Explorer window and add a "App_GlobalResources" folder to your project by right clicking and selecting "Add ASP.NET Folder" option. It will add "App_GlobalResources" folder to your project. Now here right click and add Resource File and give it the name "Strings.resx"; it will add a resource file.

Step 4: Open that "String.resx" file and add a name to it and a value that you want on the first page load. We will give some English values here for the first time page load. Here we will add some fields like AboutMe, Desc, Header, Eng, Hindi, Marathi and also give some default values like About Me, Hi, Localization In Asp.Net and the two values for Hindi and Marathi taken from gmail option.

Step 5: Like in Step 4, create three more files for English, Hindi and Marathi and give the name with culture code like Strings.en-US.resx, Strings.hi-IN.resx, Strings.mr-IN.resx respectively and add same name and the different values with respect to language.

Step 6: Now open the code file of Default page and import the namespaces given bellow.

C#
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Reflection;

Step 7: Now in global declaration section of the .cs file, create an instance of the ResourceManager and CultureInfo classes.

.CS code:
C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Reflection;

public partial class _Default : System.Web.UI.Page 
{
    ResourceManager rm;
    CultureInfo ci;
    private void LoadString(CultureInfo ci)
    {

   Label1.Text = rm.GetString("AboutMe", ci);
     Label2.Text = rm.GetString("Desc", ci);
     Button1.Text = rm.GetString("Hindi", ci);
   Label3.Text = rm.GetString("Header", ci);
   Button2.Text = rm.GetString("Marathi", ci);
        Button3.Text = rm.GetString("Eng", ci);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            rm = new ResourceManager("Resources.Strings", 
                     System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }
        else
        {
            rm = new ResourceManager("Resources.Strings", 
                     System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
        LoadString(Thread.CurrentThread.CurrentCulture);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        LoadString(Thread.CurrentThread.CurrentCulture);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("mr-IN");
        LoadString(Thread.CurrentThread.CurrentCulture);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Rakesh Aytha13-May-13 21:40
Rakesh Aytha13-May-13 21:40 
Questionupdate panel issue during multilanguage using asp.net Pin
amruta khare7-Oct-12 20:02
amruta khare7-Oct-12 20:02 
GeneralMy vote of 5 Pin
Anuj Banka17-May-12 22:39
Anuj Banka17-May-12 22:39 
QuestionIT IS GOOD Beginners.Good Job !!! :) Pin
Anuj Banka17-May-12 22:38
Anuj Banka17-May-12 22:38 
Questionnon-serious Pin
AbdullaMohammad3-Apr-12 5:55
AbdullaMohammad3-Apr-12 5:55 
AnswerRe: non-serious Pin
chetan virkar4-Apr-12 18:44
chetan virkar4-Apr-12 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.