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..

9 Comments »

  1. Hi, nice post. I have been thinking about this issue,so thanks for posting. I will definitely be subscribing to your site.

    Comment by How I Lost Thirty Pounds in Thirty Days — May 3, 2009 @ 11:43 pm | Reply

  2. Hi, good post. I have been pondering this issue,so thanks for writing. I’ll certainly be coming back to your blog.

    Comment by How I Lost Thirty Pounds in Thirty Days — May 4, 2009 @ 1:36 pm | Reply

  3. Hi,

    Very useful post,but i have one question that,how can insert value field on hindi resource file which is appropriat with “Hi and Hello” word,how can we identify that this text display “Namaste aur apka swagat hai” in hindi language.

    Thanks.

    Comment by Kaushal — May 27, 2009 @ 6:30 am | Reply

    • Hi Kaushal,
      What i did currently i used google hindi translator just typed “Namaste aur aap ka swagat hai” in resource file.
      now resource file having resource key or you can say a id which identify for which control we are writing the test.
      in this manner it handle.
      if you have further query just post a comment with detail.
      thanks
      Rajat :)

      Comment by indiandotnet — May 28, 2009 @ 6:36 pm | Reply

  4. Hi There,

    Text in resx files for hindi is coming as square boxes.do we need to install any language pack or any configuration??

    Comment by Plaban — July 15, 2009 @ 10:05 am | Reply

  5. Hi Plaban,
    The Square box is hindi font.

    Thanks
    Rajat

    Comment by indiandotnet — July 15, 2009 @ 11:46 am | Reply

  6. Thanx dear for suggesting me another method to create hindi web side, acutly i use unicode to create hindi website

    Comment by Devesh — December 5, 2009 @ 7:22 am | Reply

  7. Hi, nice post
    i m developing a hindi web site i need help how to handel unicode fonts for hindi web site please help.
    asp.net + c# (Web Site)+ MSSQL 2005
    the Text Field we have taken as Nvarcahr
    pls helpfor the above.

    From
    Vaibhav

    Comment by Vaivhav — December 17, 2009 @ 9:51 pm | Reply

    • Hi Vaibhav,
      Thanks for your comment.
      I will soon you update with a sample code.
      Thanks
      Rajat

      Comment by indiandotnet — December 18, 2009 @ 4:02 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.