Friday, February 24, 2006

ASP.NET 2.0 Localization - using Visual Studio 2005

Lot of improvements have happened in ASP.NET 2.0 with regard to localizing applications.

This article will take you step by step in localizing your ASP.NET applications.For the scope of the article, we will maintain all localization entries in .resx file, although you can also maintain this using database.

ASP.NET 2.0 provides the feature of automatically localizing your pages and also manual localization. Let us look at each of these.

The following are the steps for automatic localization through VS 2005:

1. Complete your ASP.NET page with all controls.
2. Switch to design view of your page from your VS 2005 IDE.
3. From the Tools menu, click Generate Local Resource.
This will generate all resource strings needed for all controls on your page.
4. Notice that your working folder will now have another folder added to it, by name App_LocalResources. This will contain the resource file for the page for which you generated Local resources.

If you now look at the aspx code, you will find that, every control on the page that can be localizable has a meta:resourcekey attribute added to it.

Now just in case you find that the meta:resourcekey attribute has not been added to the text (this happens quite rarely - one place where I found was the Next - Previous text in a GridView), all you need to do is to add Key- Value entry in the local resource file, say,YourPage.aspx.resx and then add this attribute to the control:
NextPageText="<%$ Resources:Next %>"

Manual Localization:
In cases where you want to dynamically assign a localized text to a string, the following are the steps:


1. Right click on your Web Project, point to Add ASP.NET Folder and then click App_GlobalResources.

2. Right click on the folder, and then click Add new Item.
3. Choose Resource file under Templates, and name the new file as GlobalResources.resx.
4. Add a Key - Value pair in this .resx file, say, with Key as FName and Value as First Name.
5. Open the code-behind of the page in your application where you want to access this Key.
6. Suppose you want to assign it to a label control, you must call the resource value this way:
lblFName.Text = Resources.GlobalResources.FName;


In the next article, we will discuss on some more points on localization configurations.