<a href="http://webabcd.blog.51cto.com/1787395/344827" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100953" target="_blank">[源碼下載下傳]</a>
溫故知新ASP.NET 2.0(C#)(5) - Localization(本地化,多語言)
介紹
聲明性資源表達式可使您的應用程式使用多種語言,而不必手動編寫代碼來檢索資源并在頁中進行替換。您隻需使用 ASP.NET 2.0 中新提供的表達式文法即可對資源替換進行定義。ASP.NET 2.0 支援标準的 resx 檔案格式自動進行運作時資源檢索。
關鍵
1、Culture - 決定各種資料類型是如何組織,如數字與日期;UICulture - 決定了采用哪一種本地化資源,也就是使用哪種語言。在頁的@Page指令中或者配置檔案的<system.web><globalization>元素中設定(另外該元素内還可以設定屬性requestEncoding,responseEncoding,fileEncoding)。Culture="en-us"和Culture="auto:en-us"的差別在于,後者會先自動比對,無法自動比對則用en-us
2、HTTP 允許浏覽器使用“接受語言”(Accept-Language) HTTP 請求标頭字段将一個首選語言清單發送到 Web 伺服器。在IE中選擇工具 - Internet 選項 - 語言
3、web.sitemap應用本地化的時候設定<siteMap>的屬性enableLocalization="true"。通路全局資源:$Resources: 全局資源名, 資源内的key, 預設值;或者resourceKey="web.sitemap.resx檔案中的key"
4、程式設計方式處理用GetGlobalResourceObject() 和 GetLocalResourceObject()
5、程式設計設定Culture 和 UICulture請重寫InitializeCulture(),對 Thread.CurrentThread.CurrentCulture 和 Thread.CurrentThread.CurrentUICulture進行設定
6、通路全局資源:$ Resources:全局資源名,資源内的key;顯式通路本地資源:$ Resources:key.屬性;隐式通路本地資源:meta:resourcekey="key"。
示例
本地化測試
Localization/Test.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Localization_Test" Title="本地化測試" Culture="en-us" UICulture="en-us"
meta:resourcekey="Title" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
目前語言:
<asp:Label ID="lblCurrentCulture" runat="Server" />
</p>
目前時間:
<%=DateTime.Now %>
隐式:
<asp:Label ID="lbl" runat="server" meta:resourcekey="lbl" /></p>
顯式:
<asp:Label ID="lbl2" runat="server" Text="<%$ Resources:lbl.Text %>" ToolTip="<%$ Resources:lbl.ToolTip %>" />
全局:
<asp:Label ID="lbl3" runat="Server" Text="<%$ Resources:MyGlobal,GlobalText %>" />
編碼方式(全局資源):
<asp:Label ID="lbl4" runat="server" />
編碼方式(本地資源):
<asp:Label ID="lbl5" runat="server" />
Localize控件方式(Label控件到用戶端會解析成<span>,而Localize到用戶端後就是解析成其所包含的文字):
<asp:Localize ID="AboutUs" runat="server" meta:resourcekey="AboutUs"></asp:Localize>
<a href="?currentculture=zh-cn">中文</a>
<a href="?currentculture=en-us">英文</a>
注:<br />
Culture - 決定各種資料類型是如何組織,如數字與日期<br />
UICulture - 決定了采用哪一種本地化資源,也就是使用哪種語言
</asp:Content>
Localization/Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Localization_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 以下一看就懂
lbl4.Text = Resources.MyGlobal.GlobalText;
// lbl4.Text = (string)GetGlobalResourceObject("MyGlobal", "GlobalText");
lbl5.Text = (string)GetLocalResourceObject("lbl.Text");
lblCurrentCulture.Text = System.Globalization.CultureInfo.CurrentCulture.Name;
}
protected override void InitializeCulture()
// 擷取目前Culture的值
string s = Request.QueryString["currentculture"];
if (!String.IsNullOrEmpty(s))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(s);
}
}
從資源檔案讀圖檔
Localization/Image.aspx.cs
public partial class Localization_Image : System.Web.UI.Page
// 一看就懂
System.Drawing.Bitmap img = (System.Drawing.Bitmap)GetGlobalResourceObject(
"MyGlobal",
System.Globalization.CultureInfo.CurrentCulture.Name.ToLower().Replace("-", "_")
);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(ms.ToArray());
img.Dispose();
ms.Dispose();
ms.Flush();
Web.sitemap本地化摘要
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" enableLocalization="true">
<siteMapNode url="" title="" description="">
<siteMapNode url="~/Default.aspx" title=" $Resources: MyGlobal, home, 預設值" description="首頁" />
<siteMapNode url="" resourceKey="Localization" />
</siteMapNode>
</siteMap>
資源檔案的内容見源碼
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344881,如需轉載請自行聯系原作者