1)创建ASP.Net Web应用程序“WebGlobalizationTest”
2)创建web窗体“Default.aspx”并在该页面中添加一个Button控件
Default.aspx页面的代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebGlobalizationTest._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html>
3)为“Default.aspx”页面生成本地资源
选择:工具—生成本地资源(设计视图)
你将看到生成一个文件夹“App_LocalResources”和里面有一个资源文件“Default.aspx.resx”
打开default.aspx页面发现代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebGlobalizationTest._Default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" meta:resourcekey="Button1Resource1" /> </div> </form> </body> </html>
注意这里:meta:resourcekey="PageResource1" 和meta:resourcekey="Button1Resource1" 这就表示这里的文字是从资源中读取的。
4)编写Default.aspx.resx
5)添加资源文件“Default.aspx.en-us.resx”
复制—粘贴Default.aspx.resx文件然后重命名为Default.aspx.en-us.resx
6)编辑Default.aspx.en-us.resx
7)去掉ASP.Net的自动选择。从Default.aspx中去除:
Culture="auto" UICulture="auto"
Default.aspx页面代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebGlobalizationTest._Default" meta:resourcekey="PageResource1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" meta:resourcekey="Button1Resource1" /> </div> </form> </body> </html>
8)Default.aspx.cs代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Globalization; namespace WebGlobalizationTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected override void InitializeCulture() { if (Request.QueryString["cultureName"] != null) { string langageType = Request.QueryString["cultureName"].ToString(); CultureInfo culture = new CultureInfo(langageType); System.Threading.Thread.CurrentThread.CurrentUICulture = culture; } base.InitializeCulture(); } } }
9)创建web窗体“WebForm1.aspx”
WebForm1.aspx.cs代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebGlobalizationTest { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //方法1 //Response.Redirect("Default.aspx?cultureName=zh-cn"); //方法2 Response.Redirect("Default.aspx?cultureName=en-us"); } } }
方法1和方法2切换事件web国际化功能