Advertisment

Localization with ASP.NET 2.0

author-image
PCQ Bureau
New Update

If you are a seasoned Web developer, you will have surely come across clients who require multi-lingual versions of their websites. Localization, as this is called, is one of the best ways to reach a larger audience by presenting content in the recipient's own language. This sort of localization has a lot of uses. You can reach international users and also many users within India who can read and write only in their local languages.

Advertisment
Direct

Hit!
Applies to:

Web developers
USP:

Makes displaying your websites in multiple languages easy
Links:

http://msdn.microsoft.com  

There are several ways of doing this. Sets of static HTML pages, each in a different language, can be set up. However, the problems of this method are quite obvious. Both ASP and ASP.NET have had other methods of performing this task. But neither of them has come close to the ease with which ASP.NET 2.0 allows you to do this.

ASP.NET 2.0 also uses resource files, just as ASP.NET 1.0. However, the method by which the resources are actually used differs. While, ASP.NET 1.0 required importing some namespaces, using a ResourceManager object and more arcane stuff, ASP.NET 2.0 makes creating a localized website a breeze. So let's take a look at how it's done.

Advertisment

A simple Web page with a Label and

DropDownList that we will localize to Hindi

Localization in VS.NET 2005



When using VS.NET 2005 in the Web developer profile, you first need to create or open the Web application you wish to localize. Once the application is loaded, you need to create a new page with a couple of controls on it. Drag and drop a Label and a DropDownList control on the page. Set their properties as shown in the table on the right.

Add a new item to the project by right-clicking on the project and selecting Add New Item. Select Assembly Resource File from the templates list. Name the file as strings.resx. Once the file opens, enter the following in the table.

Advertisment
Name Value  
lblColor Please

select your favorite color:  
Color1 White  
Color2 Red  
Color3 Green  
Color4 Blue  
Color5 Yellow  
Color6 Black  

Now comes the interesting part. Create a new folder in the project by right-clicking on the project name and selecting New Folder. Call this folder Code. This is a special new folder in ASP.NET 2.0 that allows the storing of files that are compiled dynamically at run-time. Drag and drop the strings.resx file that you created into this folder.  

Next, double click on the page and enter the following in the Page_Load event.

Advertisment

The

Hindi resource file that supplies the Devnagri strings is created

as above

lblColor.Text = Resources.strings.lblColor



ddColor.Items.Add(Resources.strings. Color1)


ddColor.Items.Add(Resources.strings. Color2)


ddColor.Items.Add(Resources.strings.


Color3)


ddColor.Items.Add(Resources.strings.


Color4)


ddColor.Items.Add(Resources.strings.


Color5)


ddColor.Items.Add(Resources.strings.Color6)







Save the file and view it in a browser. You will see that the Label and the DropDown have taken in the supplied text from the resource.  

Advertisment

To localize this site, add a new resource file in the Code folder. The name of the file remains the same, but with a suffix that denotes the encoding for the language you wish to use in the format ..resx. For instance, to denote Hindi, the file must be named strings.hi-IN.resx. Now, enter the same names as in the original table and enter the equivalent texts in the value for each in Hindi.  

Property Label DropDownList
Name LblColor DdColor
Text -
Font-size 18pt 18pt
Font-bold TRUE FALSE
Items  -

This becomes a localized site immediately. To check whether the site is working correctly (that is, to view the output on a US English OS that most of us run), in the @Page directive of the ASPX file, add the attribute UICulture=?hi-IN?. Save the file and view it again. You will see the Label and the DropDown items in Hindi. As you can see, localizing a page just requires you to dynamically assign the text to each control and create the appropriate resource files. Of course, the magic behind this is the new Code folder in ASP.NET 2.0.

Advertisment

The localized Web page, now in Hindi ? without writing any extra code!

So, go ahead and make your websites reach a larger audience by using the quick and easy method of localization.  

Vinod Unny



Enterprise InfoTech

Advertisment