here are so many new features to discuss and master in ASP.NET, this article series may never be finished. Thus far, we've covered the following:
A La Carte, Part 2: Build Deep, Flexible Navigation Using Four New Controls
Learn to Use the New Data Source Controls in ASP.NET 2.0
Using the New GridView Control in ASP.NET 2.0
Track Your Site Statistics Using ASP.NET's Site Counters Service
This time, I am going to focus on the new Profile service in ASP.NET 2.0 that allows you to build personalizable Web sites. In addition, I will take this opportunity to show you how ASP.NET has simplified the globalization and localization of Web applications.
To illustrate these new features, I will first build an ASP.NET 2.0 Web application that shows how localization is done in ASP.NET 2.0. This application will allow users to choose their preferred language. (In the example, a choice of English or Chinese is offered.)
Once the user has selected a preferred language, you can design your site to display in that language each time the user visits. I'll show you how to provide that functionality using the Profile service.
Localizing a Web Application
|Figure 1. Control: Populate the default form with the four controls shown. |
To begin, I'll first build a simple ASP.NET 2.0 Web application using Visual Studio 2005 (Beta 1). I'll populate the default form with the following controls (see Figure 1
Populate the DropDownList control with the following items and values:
||Display the page in English
||Display the page in Chinese
||Display the page using the language set in IE’s language preference.
Once the form is populated, you want to create an external resource file that contains all the localized language version information used by the form, such as dates, times, and currency information. Visual Studio 2005 will generate this file for you when you select Tools -> Generate Local Resource (see Figure 2
|Figure 2. Think Local: You have to generate the local resources your application will need to perform a translation. |
Once the resources are generated, you will find a new folder named LocalResources under your project in Solution Explorer. The Default.aspx.resx
file contains resources used by Default.aspx
for the default culture (which is English). Right-click on this file and select Copy, and then right-click on LocalResources and select Paste. This new folder will contain the resources for your other languagein my example, Chinese. I named the new file Default.aspx.zh-CN.resx
(see Figure 3
|Figure 3. Adding Languages: The LocalResources folder contains the resource files for the languages you support.|
shows the content of these two resource files. I have changed the content of the Default.aspx.zh-CN.resx
file to display Chinese characters. (To follow along you will either need to configure Windows for Chinese input or use a third-party language add-in. For simplicity, you may elect to use a Western language, such as French or Spanish, instead). During runtime, the ASP.NET controls will automatically retrieve the strings stored in the resource files (according to the language preference set in the Web browser). This is known as Implicit Localization
|Figure 4. West to East: The left side, Default.aspx.resx, contains local resources for English and the right, Default.aspx.zh-CN.resx, contains the Chinese equivalents.|
In ASP.NET 2.0, you can simply add two new attributes"Culture" and "UICulture"to the @Page directive of a page and the ASP.NET runtime will automatically localize your application based on the resource files you have created:
<%@ Page Language="VB"
ClassName="Default_aspx" meta:resourcekey="PageResource1" %>
To test the application, open Internet Explorer and set your language preference to Chinese by going to Tools -> Internet Options. Click on the Languages… button and select Chinese (see Figure 5
Once you've changed your language preference in the browser, you're done. The ASP.NET application will now display in Chinese (see Figure 6).