WEB中实现国际化(一)

最近这几天一直在写国际化和配置国际化的工具,现在将我实现国际化的思路和想法帖出来:本文主要讲一下母版页、内容页和常规页的国际化

项目结构:

一、将资源文件用XML文件(键值)的形式存储:

ContentPage.xml

<?xml version="1.0" encoding="utf-8" ?>
<Resources>
<Item key="PageTitle" zh-cn="首页" en-US="Home Page"></Item>
<Item key="HeaderText" zh-cn="页头文字" en-US="Header Text"></Item>
<Item key="BodyText" zh-cn="Body文字" en-US="Bodt Text"></Item>
</Resources>

GeneralPage.xml

<?xml version="1.0" encoding="utf-8" ?>
<Resources>
<Item key="PageTitle" zh-cn="首页" en-US="Home Page"></Item>
<Item key="BodyText" zh-cn="Body文字" en-US="Bodt Text"></Item>
</Resources>

Site.xml

<?xml version="1.0" encoding="utf-8" ?>
<Resources>
<Item key="LanguageSwitch" zh-cn="语言切换" en-US="Language Switch"></Item>
<Item key="FooterText" zh-cn="上海盛大网络发展有限公司" en-US="SNDA Co,Ltd "></Item>
<Item key="Logo" zh-cn="Images/snda_logo_ch-zn.jpg" en-US="Images/snda_logo_en-US.jpg"></Item>
</Resources>

二、在Global.asax中每次请求的时候,即(Application_BeginRequest事件)中添加

///<summary>
/// 初始化国际化设置
///</summary>
void InitI18NSettings() {
string currentLanguageType = WebUtility.ReadCookies(Request, WebUtility.CURRENT_LANGUAGE_TYPE_KEY);
if (string.IsNullOrEmpty(currentLanguageType)) {
currentLanguageType = WebUtility.DefaultLanguage;
WebUtility.WriteCookies(Response, WebUtility.CURRENT_LANGUAGE_TYPE_KEY, currentLanguageType);
}
Application[WebUtility.CURRENT_LANGUAGE_TYPE_KEY] = currentLanguageType;
}
void Application_BeginRequest(object sender, EventArgs e) {
InitI18NSettings();
}

三、母版页与内容页的国际化

1)、母版页国际化基类

using System;
using System.Collections.Generic;
public abstract class MasterPageI18N : System.Web.UI.MasterPage {
private string _ResourcesFilePath;
protected Dictionary<string, string> Resources {
get {
if (string.IsNullOrEmpty(_ResourcesFilePath))
throw new Exception("请指定资源文件(.XML)文件路径!");
if (ViewState["__Resources"] == null) {
Dictionary<string, string> _Resources =
WebUtility.GetResources(_ResourcesFilePath, Server, Application);
ViewState["__Resources"] = _Resources;
return _Resources;
}
return (Dictionary<string, string>)ViewState["__Resources"];
}
set {
ViewState["__Resources"] = value;
}
}
protected virtual void SetResourcesFilePath(string resourcesFilePath) {
_ResourcesFilePath = resourcesFilePath;
}
}

2)、母版页site.master文件

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css"/>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<asp:Image ID="imgLogo" runat="server"/>
</div>
<div class="loginDisplay">
<asp:Label ID="lblLanguageSwitch" runat="server"></asp:Label>
:
<asp:LinkButton ID="btn_zh_cn" runat="server" CommandName="zh-cn"
OnClick
="btnSwitchClick">中文</asp:LinkButton>&nbsp;|&nbsp;
<asp:LinkButton ID="btn_en_US" runat="server" OnClick="btnSwitchClick"
CommandName
="en-US">English</asp:LinkButton>
</div>
<div class="clear hideSkiplink">
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
<asp:Label ID="lblFooterText" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

注意以上中英文切换就里都调用了btnSwitchClick事件,用CommandName的值来区分。

3)母版页site.master.cs文件

using System;
using System.Web.UI.WebControls;
public partial class SiteMaster : MasterPageI18N {
protected void Page_Load(object sender, EventArgs e) {
SetResourcesFilePath(@"Resources\Site.xml");
if (!IsPostBack) {
Load();
}
}
protected void btnSwitchClick(object sender, EventArgs e) {
LinkButton lnkBtn = ((LinkButton)sender);
if (null != lnkBtn) {
WebUtility.WriteCookies(Response, WebUtility.CURRENT_LANGUAGE_TYPE_KEY, lnkBtn.CommandName);
Application[WebUtility.CURRENT_LANGUAGE_TYPE_KEY] = lnkBtn.CommandName;
Resources = null;
}
Load();
}
private void Load() {
lblLanguageSwitch.Text = Resources["LanguageSwitch"];
lblFooterText.Text = Resources["FooterText"];
imgLogo.ImageUrl = imgLogo.ResolveUrl(Resources["Logo"]);
}
}

四、内容页实现国际化

1)、内容页国际化基类

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public abstract class ContentPageI18N : System.Web.UI.Page {
private string _ResourcesFilePath;
protected Dictionary<string, string> Resources {
get {
if (string.IsNullOrEmpty(_ResourcesFilePath))
throw new Exception("请指定资源文件(.XML)文件路径!");
if (ViewState["__Resources"] == null) {
Dictionary<string, string> _Resources =
WebUtility.GetResources(_ResourcesFilePath, Server, Application);
ViewState["__Resources"] = _Resources;
return _Resources;
}
return (Dictionary<string, string>)ViewState["__Resources"];
}
set {
ViewState["__Resources"] = value;
}
}
protected virtual void SetResourcesFilePath(string resourcesFilePath) {
_ResourcesFilePath = resourcesFilePath;
}
protected override void OnInitComplete(EventArgs e) {
base.OnInitComplete(e);
LinkButton btn_zh_cn = (LinkButton)Page.Master.FindControl("btn_zh_cn");
LinkButton btn_en_US = (LinkButton)Page.Master.FindControl("btn_en_US");
if (null != btn_zh_cn)
btn_zh_cn.Click += SwitchClick;
if (null != btn_en_US)
btn_en_US.Click += SwitchClick;
}
protected void SwitchClick(object sender, EventArgs e) {
LinkButton lnkBtn = ((LinkButton)sender);
if (null != lnkBtn) {
WebUtility.WriteCookies(Response, WebUtility.CURRENT_LANGUAGE_TYPE_KEY, lnkBtn.CommandName);
Application[WebUtility.CURRENT_LANGUAGE_TYPE_KEY] = lnkBtn.CommandName;
Resources = null;
}
Load();
}
protected virtual void Load() {
}
}

注意这里,内容页的时候,基类重写了OnInitComplete事件,当页面点击中文|英文的时候,首先先去执行母版页的btnSwitchClick事件,然后再执行内容页的事件,在内容页里OnInitComplete事件中注册事件并让他重新加载内容页资源文件。

2)、内容页aspx页代码

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ContentPage.aspx.cs"
Inherits
="ContentPage"%>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Label ID="lblHeaderText" runat="server"></asp:Label>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblBodyText" runat="server"></asp:Label><br />
<br />
</asp:Content>

3)、内容页aspx.cs页代码

using System;
public partial class ContentPage : ContentPageI18N {
protected void Page_Load(object sender, EventArgs e) {
SetResourcesFilePath(@"Resources\ContentPage.xml");
if (!IsPostBack) {
Load();
}
}
protected override void Load() {
base.Load();
this.Title = Resources["PageTitle"];
lblHeaderText.Text = Resources["HeaderText"];
lblBodyText.Text = Resources["BodyText"];
}
}

4)、效果图

这是中文的时候。

这是点击英文的时候。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值