oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Localization in ASP.NET 2.0

by Wei-Meng Lee

The Web is an international place. Why shouldn't your websites be ready for international visitors? Isn't that was the first two Ws ("World" and "Wide") in WWW are for? With the introduction of ASP.NET 2.0, Microsoft aims to make it easier to localize your website for individual users, no matter where they hail from. In this article, I will show you how you can localize your ASP.NET 2.0 web applications. You will enable the application to display in two languages--English and Chinese.

Note: For the basics of globalization and localization, refer to my earlier articles on this topic:

New Auto-Culture Handling in ASP.NET 2.0

ASP.NET 2.0 comes with a new auto-culture handling feature to make the task of localizing your application easier. Auto-culture handling can be enabled for each page by including the Culture="auto" and UICulture="auto" attributes in the Page directive of each page. Once enabled, the ASP.NET runtime will automatically map Accept-Language headers to CultureInfo objects and attach them to the current thread (unlike ASP.NET 1.x, where you need to do this manually). As a developer, you simply need to prepare the resources for the different cultures you want to support in your application. ASP.NET will then do the work of loading the appropriate resources for each culture as they are needed.

Implicit Localization

Let's use Visual Studio 2005 and create a new website project by going to File -> "New Web Site…." Name the project C:\Localization. For this application, I will use the Visual Basic 2005 language.

Populate the default web form, Default.aspx, with the controls, as shown in Figure 1.

figure 1
Figure 1. Populating the form

Once the controls on the form are populated and named, you will generate the resources (such as the text displayed by the controls) so that they can be grouped into a single file. To do so, go to Tools -> Generate Local Resource (see Figure 2).

figure 2
Figure 2. Generating local resources

A new folder named App_LocalResources will now appear under your project in Solution Explorer (see Figure 3). In the folder, you will find the Default.aspx.resx file, which contains all of the resources used by your form. Duplicate a copy of this resource file so that you can use it for another language (Chinese, in this case). Right-click on Default.aspx.resx and select Copy. Right-click on App_LocalResources and then select Paste. Rename the newly pasted file Default.aspx.zh-CN.resx (zh-CN is the culture code for Chinese in China).

figure 3
Figure 3. The App_LocalResources folder

The Solution Explorer should now look like Figure 4.

figure 4
Figure 4. The resource file for the Chinese language

Pages: 1, 2, 3, 4

Next Pagearrow