65.9K
CodeProject 正在变化。 阅读更多。
Home

全球化与本地化

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2投票s)

2007年12月10日

CPOL

1分钟阅读

viewsIcon

32150

downloadIcon

125

使用主页实现多区域支持。

引言

在 ASP.Net 中实现全球化和本地化概念,可以使开发人员和最终用户通过特定的或通用的区域性内容或信息来增强 Web 应用程序的功能。全球化使开发人员能够设计和开发适用于多种文化的应用程序,而本地化则允许为给定的文化和区域定制应用程序。 在本文档和附带的示例文件中,包含一些如下技术术语的解释:

背景

ASP.Net 2.0 可以为特定的浏览器渲染本地化的 HTML。它会检查 "Accept-Language" HTTP 标头以识别浏览器设置。要执行本地化,我们可以简单地在 @Page 中添加属性,或在 web.config 文件中添加全球化部分。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Culture="auto" UICulture="auto" %>

或者

<!--// Web.config section under system.web-->

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

"Culture" 和 "UICulture" 之间的区别在于,"Culture" 控制日期、数字和货币的格式,而 "UICulture" 用于资源加载。

使用代码

我创建了一个示例来演示两种文化和语言的实现,例如英语和阿拉伯语。 注意:您会发现阿拉伯语没有任何含义,只是为了在您的应用程序中使用阿拉伯语。

在这个示例中,有一个名为 Default.aspx 的页面,它由基页类继承,该基页类管理用户请求时文化的变化。当用户按下更改文化的按钮(例如英语或阿拉伯语)时,基页类会将当前文化线程更改为用户指定的文化。

            //

            // Any source code blocks look like this

            //

        
protected void lnkEnglish_Click(object sender, EventArgs e)

{

this.CurrentCulture = "en-US";

//pageDiv.Attributes.Add("dir", "ltr");


base.InitializeCultures();

}
where as base class having the manual mechanism to change the contents on the web page to 
the specified culture.

in Default2.aspx there is a master page, which contains two link buttons for 
arabic and english. the change in the culture is managed by master with the same manual mechanism.
and in Default3.aspx file the culture is implemented by the generating the local Resource from 
Visual studio Menu -> Tools -> Generate Local Resource. this option generates the local resource for 
the current page with current page name like Default3.aspx.resx. I leave this page to readers to inherit
"cs">this page with master page(having two link buttons for English and Arabic) and just change the culture 
by using the following methods. 

protected void lnkEnglish_Click(object sender, EventArgs e)

{

base.UICulture = "en-US";

Session["UIc"] = "en-US";

InitializeCulture();

}

protected void lnkArabic_Click(object sender, EventArgs e)

{

base.UICulture = "ar-SA";

Session["UIc"] = "ar-SA";

InitializeCulture();

}

 

protected override void InitializeCulture()

{

try

{

Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["UIc"].ToString());

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

//base.InitializeCulture();


}

catch (Exception ex)

{

}

}
You'll see a differnce from other two pages that this implementation is automatic. you donot need to 
do it manually as you've done in previous two pages.
NOTE : Uploaded are two files, One is having WebFiles and other contains a solution file. plz change a soltuion file settings by editing it into a notepad according to ur system local path for this project.
全球化与本地化 - CodeProject - 代码之家
© . All rights reserved.