戰地連結︰ Home My Flickr NBA.com About

2008年8月20日星期三

Globalization of ASP.NET with Resources File

Overview

Resources files can be used to provide multi-languages support in ASP.NET web applications. The web app can be set to display different languages based on the settings of client browser. Following shows how to use resources files in a web application project.

Reference

  1. ASP.NET Web Page Resources Overview
  2. Resource Files in ASP.NET 2.0
  3. CultureInfo Class
  4. globalization Element (ASP.NET Settings Schema)

Example

In the following example I will create a web application project in Visual Studio 2008 and add English & Traditional Chinese support for a simple page containing only one Label.

Create the Web Application Project

Note: In order to make the resources files editable even after the application is published, we have to use "ASP.NET Web Application" as the project template.

  1. In VS 2008, select "File" -> "New" -> "Project"
  2. In "Project types", select "Visual C#" -> "Web".
  3. In "Template", select "ASP.NET Web Application". Enter the Name of the web and the solution

Add Global Resources files (for default language: English) to the application

  1. Right-click on the web application project
  2. In the appeared menu, select "Add" -> "Add ASP.NET Folder" -> "App_GlobalResources", a folder named "App_GlobalResources" created
  3. Right-click on folder "App_GlobalResources"
  4. In the appeared menu, select "Add" -> "New Item..."
  5. In "Add New Item" window, select "Resources File", name it as "Resource.resx"
  6. Open "Resource.resx" and add a row with
    Name: TestString
    Value: Test

Add another Global Resources files for Trad. Chinese

  1. Right-click on folder "App_GlobalResources"
  2. In the appeared menu, select "Add" -> "New Item..."
  3. In "Add New Item" window, select "Resources File" under "Visual C#" category, name it as "Resource.zh-Hant.resx"
    Note: The phase between "Resource" & "resx" indicate the culture represented by this resources file. In this case, "zh-Hant" is the culture for "Traditional Chinese" which includes Locales for "Hong Kong", "Taiwan" etc.
  4. Open "Resource.zh-Hant.resx" and add a row with
    Name: TestString
    Value: 測試

Configure "web.config" to change Language based on client browser's setting

  1. Open file "web.config"
  2. Add the following code under "system.web" tab

<globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto"/>

Add an ASP.NET page with a Label to see the result

  1. Right-click on the web application project
  2. In the appeared menu, select "Add" -> "New Item..."
  3. In "Add New Item" window, select "Web Form", name it as "Test.aspx"
  4. Open "Test.aspx", add a Web Control Label to it with the following code:

<asp:Label ID="Label1" runat="server" Text="<%$ Resources: Resources, TestString %>" />

Debug the project to see the result

If your browser (take IE 7 in this case) don't have any language preference setting, then the page should shows "Test". You can change the Language setting by:

  1. Select "Tools" -> "Internet Options"
  2. In "General" Tab, select "Languages"
  3. Click "Add" to add more preferred Languages, you can also change the priority of preferred Languages

沒有留言: