ASP.NET 2.0(C#)(5)

來源:互聯網
上載者:User

介紹

聲明性資源運算式可使您的應用程式使用多種語言,而不必手動編寫代碼來檢索資源並在頁中進行替換。您只需使用 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.