ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


Localization in ASP.NET 2.0

by Wei-Meng Lee
08/08/2005

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

ASP.NET 2.0: A Developer's Notebook

Related Reading

ASP.NET 2.0: A Developer's Notebook
By Wei-Meng Lee

Double-click on Default.aspx.zh-CN.resx to edit the resources contained within. In particular, enter the following (see Figure 5, highlighted in red) in Chinese.

figure 5
Figure 5. Entering the resources in Chinese

To use the auto-culture handling feature of ASP.NET 2.0, ensure that the following two attributes are present in the Page directive:


<%@ Page Language="VB" AutoEventWireup="false" 
   CodeFile="Default.aspx.vb" Inherits="_Default" 
   meta:resourcekey="PageResource1" 
   Culture="auto"  UICulture="auto" %>

That's it! You can now press F5 to test using Internet Explorer. By default, you should see that IE displays your page in English. To change to Chinese, in IE go to Tools -> Internet Options…. Click the Languages… button and then click the Add… button to add the "Chinese (China) [zh-cn]" language to your browser. Ensure that the newly added Chinese language is now at the top of the preferences (see Figure 6). Click OK twice to dismiss the dialog boxes.

figure 6
Figure 6. Setting language preference in IE

Refresh IE and your web page should now display in Chinese (see Figure 7).

figure 7
Figure 7. Viewing the page

To switch back to English, simply configure IE to display English as the preferred language. This method of localization that you have just seen is known as implicit localization.

Explicit Localization

Besides automatically setting the culture of the application based on the preference of the web browser, you can also explicitly set a page to display in a specific Culture. To do so, you need to use the CultureInfo class from the System.Globalization namespace. You need to set the CurrentCulture and CurrentUICulture properties of the current thread in the Page_PreInit event. The following sets the page to display in Chinese:


Protected Sub Page_PreInit( _
   ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.PreInit

    Dim lang As System.Globalization.CultureInfo
    lang = New System.Globalization.CultureInfo("zh-CN")

    System.Threading.Thread.CurrentThread.CurrentCulture = lang
    System.Threading.Thread.CurrentThread.CurrentUICulture = lang

End Sub

You also need to remove the two Culture and UICulture attributes present in the Page directive.

However, you will notice something interesting when you press F5 to test the application. Regardless of what language you set in IE, you will always see the page shown in Figure 8. Notice that the Label controls are still displaying the text in English, while the Calendar control has been changed to Chinese.

figure 8
Figure 8. Viewing the page

Unfortunately, implicit localization only works when you set the preferred language in IE. To explicitly set the culture for a page, you need to perform explicit localization. To do so, right-click on project name in Solution Explorer and select Add Folder -> App_GlobalResources Folder (see Figure 9).

figure 9
Figure 9. Adding the App_GlobalResources folder

Right-click on newly created App_GlobalResources folder in Solution Explorer and then select Add New Item…. Select Assembly Resource File (see Figure 10) and use the default name of Resource.resx. Click Add.

figure 10
Figure 10. Adding an assembly resource file to the project

Double-click on the Resource.resx file and populate it with content, as shown in Figure 11.

figure 11
Figure 11. Populating the Resource.resx file

Duplicate a copy of the Resource.resx file and name it Resource.zh-CN.resx. As usual populate its contents, as shown in Figure 12.

figure 12
Figure 12. Populating the Resource.zh-CN.resx file

Both the Resource.resx and Resource.zh-CN.resx files contain the resources for the English and Chinese cultures. In the Page_PreInit event, you use the Resources class to access the resources defined in these two resource files, as follows:


Protected Sub Page_PreInit( _
   ByVal sender As Object, ByVal e As System.EventArgs) _
   Handles Me.PreInit

    Dim lang As System.Globalization.CultureInfo
    lang = New System.Globalization.CultureInfo("zh-CN")

    System.Threading.Thread.CurrentThread.CurrentCulture = lang
    System.Threading.Thread.CurrentThread.CurrentUICulture = lang

    lblName.Text = Resources.Resource.lblName
    lblBirthday.Text = Resources.Resource.lblBirthday
    lblAddress.Text = Resources.Resource.lblAddress
    lblSalary.Text = Resources.Resource.lblSalary

End Sub

Press F5 to test the application. Your page should now display in Chinese, regardless of the language preference setting in IE (see Figure 13).

figure 13
Figure 13. Displaying the page in Chinese

Summary

Localizing your web applications has been simplified in ASP.NET 2.0. If you are targeting your application to international users, it is important that you take the extra effort to ensure that your application is world-ready. Fortunately, ASP.NET 2.0 makes it really easy for you.

Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.


Return to OnDotNet.com

Copyright © 2009 O'Reilly Media, Inc.