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

[索引页]
[源码下载]



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


作者: webabcd


介绍
声明性资源表达式可使您的应用程序使用多种语言,而不必手动编写代码来检索资源并在页中进行替换。您只需使用 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控件到客户端会解析成
&lt; span &gt; ,而Localize到客户端后就是解析成其所包含的文字):
        
< asp:Localize  ID ="AboutUs"  runat ="server"  meta:resourcekey ="AboutUs" ></ asp:Localize >
    
</ p >
    
< p >
        
< href ="?currentculture=zh-cn" > 中文 </ a >
        
&nbsp;
        
< 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
[源码下载]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值