Multi Language Web-Site In ASP.NET
Introduction:
Here we will learn how to translate asp.net pages in user selected language. The article explains step-by-step procedure to implement your website in Multiple languages.
Back-Ground:
I read so many thread on c-sharpcorner and other forums also related to multilingual web-site in asp.net. This task actually I’ve done in my project that’s the step only I’m explaining here. This article focuses on three languages which I know i.e. English, Hindi and Marathi. According to your choice language you can extend the same thing in your own languages. 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 mail body you will find some option to change language. Here on left hand side up corner changing language option is available. 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 English word the similar word in Hindi and Marathi is “Naam” & “Naav”. So let’s go to start our web site design.
Step 1:
Start New web-site with C# and give the Name as multilingual. It will give you a Default.aspx and Default.aspx.cs two files.
Step 2:
Now Design the form by putting three label and three button on form set the id to label as well as button.
Step 3:
Now go to Solution Explorer window and add “App_GlobalResources” folder in 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 name as “Strings.resx” it will add resource file.
Step 4:
Open that “String.resx” file and add some name to it and value which you want on first page load. We will give some English values here for first time page load. Here we will add some fields like AboutMe,Desc,Header,Eng,Hindi,Marathi and also given some default values like About Me, Hi, Localization In Asp.Net and the that two values for Hindi and Marathi taken from gmail option.
Step 5:
Same like 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 instance of ResourceManager and CultureInfo Class.
ResourceManager rm;
CultureInfo ci;
Step 8:
In Page_Load Event write 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 8:
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. Following I’m giving one table which contain the langauge 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.