天天看点

温故知新ASP.NET 2.0(C#)(5) - Localization(本地化,多语言)

<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指令中或者配置文件的&lt;system.web&gt;&lt;globalization&gt;元素中设置(另外该元素内还可以设置属性requestEncoding,responseEncoding,fileEncoding)。Culture="en-us"和Culture="auto:en-us"的区别在于,后者会先自动匹配,无法自动匹配则用en-us

2、HTTP 允许浏览器使用“接受语言”(Accept-Language) HTTP 请求标头字段将一个首选语言列表发送到 Web 服务器。在IE中选择工具 - Internet 选项 - 语言

3、web.sitemap应用本地化的时候设置&lt;siteMap&gt;的属性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

&lt;%@ 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" %&gt; 

&lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt; 

        &lt;p&gt; 

                目前语言: 

                &lt;asp:Label ID="lblCurrentCulture" runat="Server" /&gt; 

        &lt;/p&gt; 

                当前时间: 

                &lt;%=DateTime.Now %&gt; 

                隐式: 

                &lt;asp:Label ID="lbl" runat="server" meta:resourcekey="lbl" /&gt;&lt;/p&gt; 

                显式: 

                &lt;asp:Label ID="lbl2" runat="server" Text="&lt;%$ Resources:lbl.Text %&gt;" ToolTip="&lt;%$ Resources:lbl.ToolTip %&gt;" /&gt; 

                全局: 

                &lt;asp:Label ID="lbl3" runat="Server" Text="&lt;%$ Resources:MyGlobal,GlobalText %&gt;" /&gt; 

                编码方式(全局资源): 

                &lt;asp:Label ID="lbl4" runat="server" /&gt; 

                编码方式(本地资源): 

                &lt;asp:Label ID="lbl5" runat="server" /&gt; 

                Localize控件方式(Label控件到客户端会解析成&lt;span&gt;,而Localize到客户端后就是解析成其所包含的文字): 

                &lt;asp:Localize ID="AboutUs" runat="server" meta:resourcekey="AboutUs"&gt;&lt;/asp:Localize&gt; 

                &lt;a href="?currentculture=zh-cn"&gt;中文&lt;/a&gt; 

                &lt;a href="?currentculture=en-us"&gt;英文&lt;/a&gt; 

                注:&lt;br /&gt; 

                Culture - 决定各种数据类型是如何组织,如数字与日期&lt;br /&gt; 

                UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言 

&lt;/asp:Content&gt;

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本地化摘要

&lt;?xml version="1.0" encoding="utf-8" ?&gt; 

&lt;siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" enableLocalization="true"&gt; 

    &lt;siteMapNode url="" title=""    description=""&gt; 

        &lt;siteMapNode url="~/Default.aspx" title=" $Resources: MyGlobal, home, 默认值" description="首页" /&gt; 

        &lt;siteMapNode url="" resourceKey="Localization" /&gt; 

    &lt;/siteMapNode&gt; 

&lt;/siteMap&gt;

资源文件的内容见源码

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344881,如需转载请自行联系原作者