WEB中实现国际化(二)

五、其它页面实现国际化

1)、其它页面国际化基类

using System;
using System.Collections.Generic;
public abstract class GeneralPageI18N : 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;
}
}

2)、其它页面国际化.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GeneralPage.aspx.cs" Inherits="GeneralPage"%>
<!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:Label ID="lblBodyText" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

3)、其它页面国际化.aspx.cs代码:

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

六、WebUtility.cs

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

public class WebUtility {
public const string DEFAULT_LANGUAGE_KEY = "DefaultLanguage";
public const string CURRENT_LANGUAGE_TYPE_KEY = "CurrentLanguageType";
public const string CHINESE_LANGUAGE_KEY = "zh-cn";
public const string ENGLISH_LANGUAGE_KEY = "en-US";

public static string GetAppSettingByKey(string key) {
return System.Configuration.ConfigurationManager.AppSettings[key];
}

public static string DefaultLanguage {
get {
return GetAppSettingByKey(DEFAULT_LANGUAGE_KEY);
}
}

///<summary>
/// 写Cookies
///</summary>
///<param name="response"></param>
///<param name="key"></param>
///<param name="value"></param>
///<param name="expriesHour">设置的过期时间</param>
public static void WriteCookies(
HttpResponse response, string key, string value, int expriesHour = 24) {
response.Cookies[key].Value = value;
response.Cookies[key].Expires = System.DateTime.Now.AddHours(expriesHour);
}

///<summary>
/// 读Cookies
///</summary>
///<param name="request"></param>
///<param name="key"></param>
///<returns></returns>
public static string ReadCookies(HttpRequest request, string key) {
if (request.Cookies[key] != null)
return request.Cookies[key].Value.ToString();
return "";
}

///<summary>
/// 删除Cookies,立即过期
///</summary>
///<param name="response"></param>
///<param name="request"></param>
///<param name="server"></param>
///<param name="key"></param>
public static void DeleteCookies(HttpResponse response, HttpRequest request, string key) {
if (request.Cookies[key] != null) {
HttpCookie cookies = request.Cookies[key];
cookies.Expires = System.DateTime.Now.AddHours(-1);
response.Cookies.Add(cookies);
}
}

public static Dictionary<string, string> GetResources(string resourcesFilePath,
HttpServerUtility server, HttpApplicationState application) {
Dictionary<string, string> dic = new Dictionary<string, string>();
dynamic dicEnumerable=null;
string filePath = server.MapPath(resourcesFilePath);
string currentLanguageType = application[CURRENT_LANGUAGE_TYPE_KEY].ToString();
XDocument document = XDocument.Load(filePath, LoadOptions.PreserveWhitespace);
switch (currentLanguageType) {
case CHINESE_LANGUAGE_KEY:
dicEnumerable = (from item in document.Descendants("Resources").Descendants("Item")
let key = item.Attribute("key")
let value = item.Attribute("zh-cn")
select new {
key,
value
});
break;
case ENGLISH_LANGUAGE_KEY:
dicEnumerable = (from item in document.Descendants("Resources").Descendants("Item")
let key = item.Attribute("key")
let value = item.Attribute("en-US")
select new {
key,
value
});
break;
default: break;
}
foreach (var item in dicEnumerable) {
dic.Add((string)item.key, (string)item.value);
}
return dic;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值