现在的位置: 首页 > 综合 > 正文

逐步解说: 将Web Form网页国际化

2013年02月07日 ⁄ 综合 ⁄ 共 3271字 ⁄ 字号 评论关闭

[选摘自:http://msdn.microsoft.com/library/cht/default.asp?url=/library/CHT/vbcon/html/vbwlkwalkthroughlocalizingwebforms.asp]

当您将 Web Form 网页当地语系化时,Visual Studio 系統不会自动产生资源文件。您必須手动建立和编辑 XML 资源文件。这个主题会示范如何加入资源文件,然后使用 XML 编辑,以英文、中文来建立资源。这个主题也讨论如何编写存取这些资源的程序。

您也可將文本内容转换成资源文件。如需详细说明,请参阅文本格式的资源和资源文件生成器 (Resgen.exe)。

A. 如何手动建立并编辑资源文件

1. 建立名为 LocProject 的 ASP.NET Web 应用程序。如需详细说明,请参阅建立 Web 项目。

注意:程序中会使用项目名称 LocProject

2. 在项目中,加入新项(Add New Item...)

3. 选择 "资源文件"(Assembly Resource File) 。文件名为 strings.resx (名称中间不能有".",否则,容易错误)。资源文件 strings.resx 将会包含英文的原有资源。每当应用程序找不到更适合 UI 文化特性的资源,就会存取这些资源。

文件将加入至项目中,并在 XML 设计工具 中打开。

4. 在 "数据表"(Data Tabls:) 列表中,选择 "data"。

5. 在 "数据" (Data:) 页面中,增加2行数据 txtSearch 和 txtWelcome, 如下:

name       value
txtSearch  Search for the following text:
txtWelcome Welcome to Localization!

6. 再重复1次步骤 2 到 5,建立另外一个命名为 strings.zh-CHS.resx.resx 的资料文件,数据如下:

name       value
txtSearch  搜索字符串:
txtWelcome 欢迎本地化!

B. 如何存取资源

1. 加入 System.Resources 和 System.Globalization 命名空间。以下代码如下:

' Visual Basic
Imports System.Resources
Imports System.Globalization

Public Class WebForm1
   Inherits System.Web.UI.Page

// C#
using System.Resources;
using System.Globalization;

namespace LocProject

2. 声明 ResourceManager 类的变量 LocRM

' Visual Basic
Public Class WebForm1
   Inherits System.Web.UI.Page
   Protected LocRM As ResourceManager

// C#
namespace LocProject_CS
{
   public class WebForm1 : System.Web.UI.Page
   {
      protected ResourceManager LocRM;

3. 将以下代码加入到 WebForm1 方法 Page_Load 中,创建 ResourceManager 类的实例。ResourceManager 构造函数使用两个参数。第一个是资源文件的根名称,也就是不含文化特性和 .resx 后缀的资源文件名称。第二个参数是主要组件。

' Visual Basic
LocRM = New ResourceManager("LocProject.strings", GetType(WebForm1).Assembly)

// C#
LocRM= new ResourceManager("LocProject.strings", typeof(WebForm1).Assembly);

注意:如果您没有将项目命名为 LocProject,就应该修改资源文件的根名称。

C. 如何显示页面中的静态资源字符串

将代码放在 <form> 和 </form> 标记 (Tag) 之间:

<h3><%=LocRM.GetString("txtWelcome")%></h3>

注意: ResourceManager 中区分大小写。

D. 如何将资源字符串赋给控件的属性

1. 加入代码:

' Visual Basic
Label1.Text=LocRM.GetString("txtSearch")
Label2.Text=LocRM.GetString("txtWelcome")

//C#
Label1.Text=LocRM.GetString("txtSearch");
Label2.Text=LocRM.GetString("txtWelcome");

2. 增加 System.Threading 命名空间。

' Visual Basic
' Import this namespace at the beginning of the code module
Imports System.Threading

// C#
// Import this namespace at the beginning of the code module.
using System.Threading;

3. 将下列代码加入 Page_Load 方法的起始处,指定 Web Form 网页应按照浏览器的 Accept-Language 标题中所指定之文化特性來显示资源和格式。这段代码相当重要,因为如果沒有的话,您的 Web 应用程序就会按照服务器的文化特定设置,而这可能对 Web Form 网页的远端使用者并不适用。

' Visual Basic
' Set the culture and UI culture to the browser's accept language
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))
Thread.CurrentThread.CurrentUICulture = New CultureInfo(Request.UserLanguages(0))

// C#
// Set the culture and UI culture to the browser's accept language
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);

编译、执行就可以看到结果。控制面板/区域选项 中,如果选择:中文(中国),就会显示中文网页;如果选择其它,则会显示英文网页。

[总结步骤:]
1). 创建需要使用的资源字符串文件

2). 获取客户端的区域设置
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);

3). 创建读取资源字符串的对象
(代码一定要在 2). 之后)
' Visual Basic
LocRM = New ResourceManager("LocProject.strings", GetType(WebForm1).Assembly)
// C#
LocRM= new ResourceManager("LocProject.strings", typeof(WebForm1).Assembly);

4). 读取资源字符串,完成界面国际化
Label2.Text=LocRM.GetString("txtWelcome");

抱歉!评论已关闭.