Saturday, April 27, 2013

Multi Language Support 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.

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.

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);
    }
}

No comments:

Post a Comment