Thursday, August 22, 2013

Multi Language Web-Site in ASP.NET

Introduction:

Here we will learn how to translate ASP.Net pages in a user selected language. This article explains step-by-step how to implement your website in multiple languages.

Back-Ground:

I have read so many threads on C-SharpCorner and other forums related to multilingual web sites in ASP.Net. This task actually I've done in my project; I'm explaining only that here. This article focuses on three languages which I know i.e. English, Hindi and Marathi. You can choose what languages you extend. For typing of Hindi and Marathi text I used Gmail facility. All Indian languages are supported by Gmail. For typing in your language, open your Gmail Account and Go to Compose Mail. Here while writing the mail body you will find an option to change the language. In the upper left corner is the option for changing the language. Just select your language and type the word in English text with your language meaning it will convert that English word to your selected language. For ex. "Name" is an English word; the similar word in Hindi and Marathi is "Naam" & "Naav". So let's go to start our web site design. You can see the that gamil option in bellow image. In this I've selected "Hindi" language.
untitled.bmp

Step 1:

Start a new web site with C# and give it the Name multilingual. It will give you a Default.aspx and Default.aspx.cs two 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 decleration section of .cs file create an instance of ResourceManager and CultureInfo Class.

ResourceManager rm;
    CultureInfo ci;

Step 8:

In Page_Load Event write the following code to set default locale for our page i.e. strings.resx. as follows. As well call our resource file values in LoadString method which will take cultureinfo as parameter.

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);
        }
private void LoadString(CultureInfo ci)
    {

        lblabtme.Text = rm.GetString("AboutMe",ci);
        lbldesc.Text = rm.GetString("Desc",ci);
        Button1.Text = rm.GetString("Eng",ci);
        lblheader.Text = rm.GetString("Header", ci);
        Button2.Text = rm.GetString("Hindi",ci);
        Button3.Text = rm.GetString("Marathi",ci);
    }

Step 9:

Create click events for button for English, Hindi And Marathi and write the code/change the cultureinfo on by passing specific cultureinfo for resourcemanager.

In English_Click Event Write This Code.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
LoadString(Thread.CurrentThread.CurrentCulture);

This will change our asp.net page in Standerd En-Us language.

In Hindi_Click event write this code which will change our page into hindi language.

Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");

LoadString(Thread.CurrentThread.CurrentCulture);

And finally in Marathi_Click event write code to change the page in Marathi language.

Thread.CurrentThread.CurrentCulture = new CultureInfo("mr-IN");
LoadString(Thread.CurrentThread.CurrentCulture);

This way you are able to change the page language of your Asp.net page. The following is a table of langauges and their respective codes.

Table For Language And Code For India:

English-> en-US
Hindi-> hi-IN
Marathi-> mr-IN
Telugu-> te-IN
Gujrati-> gu-IN
Panjabi-> pa-IN
Malayalam-> ml-IN
Oriya-> or-IN
Tamil-> ta-IN
Kannad-> kn-IN

Conclusion:

In this way you can create your multilingual website in ASP.Net.



Reference :
http://www.c-sharpcorner.com/uploadfile/krishnasarala/multi-language-web-site-in-Asp-Net/


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment