重新过一遍ASP.NET 2.0(C#)(5) - Localization(本地化,多语言)
生活随笔
收集整理的這篇文章主要介紹了
重新过一遍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>
????<p>
????????當前時間:
????????<%=DateTime.Now?%>
????</p>
????<p>
????????隱式:
????????<asp:Label?ID="lbl"?runat="server"?meta:resourcekey="lbl"?/></p>
????<p>
????????顯式:
????????<asp:Label?ID="lbl2"?runat="server"?Text="<%$?Resources:lbl.Text?%>"?ToolTip="<%$?Resources:lbl.ToolTip?%>"?/>
????</p>
????<p>
????????全局:
????????<asp:Label?ID="lbl3"?runat="Server"?Text="<%$?Resources:MyGlobal,GlobalText?%>"?/>
????</p>
????<p>
????????編碼方式(全局資源):
????????<asp:Label?ID="lbl4"?runat="server"?/>
????</p>
????<p>
????????編碼方式(本地資源):
????????<asp:Label?ID="lbl5"?runat="server"?/>
????</p>
????<p>
????????Localize控件方式(Label控件到客戶端會解析成<span>,而Localize到客戶端后就是解析成其所包含的文字):
????????<asp:Localize?ID="AboutUs"?runat="server"?meta:resourcekey="AboutUs"></asp:Localize>
????</p>
????<p>
????????<a?href="?currentculture=zh-cn">中文</a>
????????
????????<a?href="?currentculture=en-us">英文</a>
????</p>
????<p>
????????注:<br?/>
????????Culture?-?決定各種數據類型是如何組織,如數字與日期<br?/>
????????UICulture?-?決定了采用哪一種本地化資源,也就是使用哪種語言
????</p>
</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
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_Image?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????//?一看就懂
????????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>
資源文件的內容見源碼
OK
[源碼下載]
聲明性資源表達式可使您的應用程序使用多種語言,而不必手動編寫代碼來檢索資源并在頁中進行替換。您只需使用 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>
????<p>
????????當前時間:
????????<%=DateTime.Now?%>
????</p>
????<p>
????????隱式:
????????<asp:Label?ID="lbl"?runat="server"?meta:resourcekey="lbl"?/></p>
????<p>
????????顯式:
????????<asp:Label?ID="lbl2"?runat="server"?Text="<%$?Resources:lbl.Text?%>"?ToolTip="<%$?Resources:lbl.ToolTip?%>"?/>
????</p>
????<p>
????????全局:
????????<asp:Label?ID="lbl3"?runat="Server"?Text="<%$?Resources:MyGlobal,GlobalText?%>"?/>
????</p>
????<p>
????????編碼方式(全局資源):
????????<asp:Label?ID="lbl4"?runat="server"?/>
????</p>
????<p>
????????編碼方式(本地資源):
????????<asp:Label?ID="lbl5"?runat="server"?/>
????</p>
????<p>
????????Localize控件方式(Label控件到客戶端會解析成<span>,而Localize到客戶端后就是解析成其所包含的文字):
????????<asp:Localize?ID="AboutUs"?runat="server"?meta:resourcekey="AboutUs"></asp:Localize>
????</p>
????<p>
????????<a?href="?currentculture=zh-cn">中文</a>
????????
????????<a?href="?currentculture=en-us">英文</a>
????</p>
????<p>
????????注:<br?/>
????????Culture?-?決定各種數據類型是如何組織,如數字與日期<br?/>
????????UICulture?-?決定了采用哪一種本地化資源,也就是使用哪種語言
????</p>
</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
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_Image?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????//?一看就懂
????????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>
資源文件的內容見源碼
OK
[源碼下載]
轉載于:https://www.cnblogs.com/hulu/archive/2007/06/27/797869.html
總結
以上是生活随笔為你收集整理的重新过一遍ASP.NET 2.0(C#)(5) - Localization(本地化,多语言)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女方嫁妆是给谁的(女方嫁妆)
- 下一篇: Javascript中的时间小议