Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/1/30 1:12:09
温故知新ASP.NET,2.0(C#)(5),-,Localization(本地化,多语言)_[Asp.Net教程]

温故知新ASP.NET 2.0(C#)(5) - Localization(本地化,多语言)_[Asp.Net教程]

介绍
声明性资源表达式可使您的应用程序使用多种语言,而不必手动编写代码来检索资源并在页中进行替换。您只需使用 ASP.NET 2.0 中新提供的表达式语法即可对资源替换进行定义。ASP.NET 2.0 支持标准的 resx 文件格式自动进行运行时资源检索。



关键
1、Culture - 决定各种数据类型是如何组织,如数字与日期;UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言。在页的@Page指令中或者配置文件的元素中设置(另外该元素内还可以设置属性requestEncoding,responseEncoding,fileEncoding)。Culture="en-us"和Culture="auten-us"的区别在于,后者会先自动匹配,无法自动匹配则用en-us


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


3、web.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" %>




目前语言:



当前时间:
<%=DateTime.Now %>



隐式:



显式:



全局:



编码方式(全局资源):



编码方式(本地资源):



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



中文
&nbsp;
英文



注:

Culture - 决定各种数据类型是如何组织,如数字与日期

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



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







资源文件的内容见源码

[源码下载]


作者:webabcd







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759