Asp.net & Sql server fundas with Rajat Jaiswal

April 24, 2009

Multi Language web site in 5 easy steps in 5 minutes

Filed under: Asp.net — indiandotnet @ 6:09 pm
Tags: , , ,

Hello friends,
Here I am with a new topic Localization Globalization. Most of the time our client require website which support multi language so in that case we have to do some extra effort. Asp.net provides simplest way of how to do this. So friends I am explaining you 5 minimum steps which help us to make a website multi language.
I am talking my favorite 3 languages Hindi, English, Spanish.
So here we go
Step 1:- The first basic step you have to create a resource file which will be your default resource file for your project.
To create resource file you have to open your file then select menu option Tool –> Generate Local resource as shown in below fig

localization1

As you done with this option what you will see that in your solution explore a new folder is added with name “App_LocalResources” which having the file name extension “.resx” means suppose if you have open default.aspx file and you say generate local resource then you will get “default.aspx.rsex” in app_LocalResouce Folder. This will be your default Resource file.
When you open this file you will find. Name & Value & comment column.
Here Name is related with your page control and what should be the value for this controls will be exist in your value column.
For me by default it’s English so there is no issue for English.
Step 2:- Create new resource file for Hindi. For this we will do just copy and paste existing resource file and rename that file and replace the value which we want in Hindi or Spanish. See below fig for it

hindirex

The things to remember here that when you saving particular language file then the language code should be there for example if I am using Spanish language then my file name will be “Default.aspx.en.resx”
And for Hindi my file name will be “Default.aspx.hi.resx”

englieshrex

spanishrex

Step3:- when you see your file default.aspx then you will find there is extra text with each of your control which is resource key.
For example see below lines here we have resource key lblWelcomeMessageResouce1 this should be present in our resource file. At run time dot net get the value from this key.

  <asp:Label ID=”lblWelcomeMessage” runat=”server” Text=”Hello and welcome “                meta:resourcekey=”lblWelcomeMessageResource1″></asp:Label>

And for drop down we having following code
<asp:DropDownList ID=”ddlLanguage” runat=”server”

                    meta:resourcekey=”ddlLanguageResource1″>

                    <asp:ListItem Text=”English” Value=”en-US” Selected=”True”

                        meta:resourcekey=”ListItemResource1″></asp:ListItem>

                    <asp:ListItem Text=”Hindi” Value=”hi-IN” meta:resourcekey=”ListItemResource2″></asp:ListItem>

                    <asp:ListItem Text=”Spanish” Value=”es-MX” meta:resourcekey=”ListItemResource3″></asp:ListItem>

                </asp:DropDownList>

Step 4: I have design my page as shown below.

pagedesign

We have to look for two basic namespace here one is Resource and another is globalization.

Once you done with the above step now you have to set the culture according to your chosen value from drop down.
For that we have to write following code
Protected Overrides Sub InitializeCulture()
Try
Dim _culture As String = Session(“Language”)
If _culture <> String.Empty Then
Dim ci As New System.Globalization.CultureInfo(_culture)
System.Threading.Thread.CurrentThread.CurrentCulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture = ci

End If

MyBase.InitializeCulture()
Catch ex As Exception
Throw ex
End Try
End Sub ‘InitializeCulture

Its just override your base class seating. In session we are putting value we have selected in drop down.
On button click event we assign the value (selected dropdown value) in the session.
In this way we get multi language web page.

Step 5:- Just run it :D

runningmode

In this way we have finished our minor task
You can download code from http://www.indiandotnetmultilanguagework.tk/

In next few days I will come back with more solid presentation.
Till than
Do Take Care your self
And enjoy coding..

Blog at WordPress.com.