zoukankan      html  css  js  c++  java
  • 温故知新ASP.NET 2.0(C#)(5) Localization(本地化,多语言)

    作者:webabcd

    介绍

    作者: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>
            <a href="?currentculture=zh-cn">中文</a>
            &nbsp;
            <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
    [源码下载]

    OK
    [源码下载]
  • 相关阅读:
    Java设计模式6
    数据库的三范式是什么?
    Mysql 索引的原理和实现
    Java Calendar
    读书-《众病之王:癌症传》-癌症的历史
    Numpy的终极备忘录
    监督学习和kNN分类初学者教程
    用PyTorch实现图像聚类
    Pandas概论
    掌握Python字典的12个例子
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2035305.html
Copyright © 2011-2022 走看看