Monday, 2 July 2012

Working With Resource Files - Multi Language

Creating Resource Files:

First of all lets create the resource files for different languages
we need to create four text files in a folder name "Resources"  for English,Hindi,Kannada and tamil as below:
(we can use .resx files also in place of text files)

1. resource.en-US.txt  :
address=Address
name=Name
select=Select
submit=Submit
translate=Translate

2. resource.hi-IN.txt :
address=पता
name=नाम
select=सेलेक्ट करिएँ
submit=सूचना का अधिकार
translate=अनुवाद

3. resource.kn-IN.txt :
address=അഡ്രസ്സ്
name=നാമേ
select=സെലക്റ്റ്
submit=ಸುಬ್ಮಿತ್
translate=അഡ്രസ്സ്

4. resource.ta-IN.txt :
address=ಸೆಲೆಕ್ಟ್
name=ನೇಮ್
select=ಅಡ್ರೆಸ್
submit=சுப்மிட்
translate=ಸೆಲೆಕ್ಟ್

Now Open the Visual studio command prompt  and type the below command :



 It will convert the text file into resources file.We have to do the same thing for all the other three files also
This way we will have four resources file of our four test files.

Creating Project:

Now create a new Project and add a folder "Resources" into that project
Now Add all the four resource files that we have created in previous step to this folder
And Design a web form as shown below :


Here in the dropdown list, add four option i.e. English(value: en-US),Hindi(value: hi-IN),
Kannada(value: kn-IN)  and Tamil(value: ta-IN)




Lets Do Some Code:

At the outset import these files :

Imports System
Imports System.Globalization
Imports System.Threading
Imports System.Resources
Imports System.Reflection

and declare these globaly :
Dim ci As CultureInfo
Dim strResourcesPath As String = Server.MapPath("Resources")

Now create these two methods:

 Private Sub ReplaceText()                                     //For fetching the respected values in different languages
        Label1.Text = rm.GetString("select")
        Label2.Text = rm.GetString("name")
        Label3.Text = rm.GetString("address")
        Label4.Text = rm.GetString("submit")
        Button1.Text = rm.GetString("translate")
  End Sub

 Protected Sub SetCulture()                                   //For setting the selected culture
        rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcesPath, Nothing)
        ci = Thread.CurrentThread.CurrentCulture
 End Sub

And for dropdownlist :

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
        Dim str As String = ddlLanguage.SelectedValue.ToString()
        Thread.CurrentThread.CurrentCulture = New CultureInfo(str)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(str)
        SetCulture()
        ReplaceText()
    End Sub


And Finally on Page Load :
 
  If Not Page.IsPostBack Then  
            Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
            //For setting the default culture to English
            Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")
            SetCulture()
            ReplaceText()
   End If

Now Finally Run the application and change the language options from the drop down list
and you will find the corresponding labels and button text changing according to the  Language selected


 

 


No comments: