asp.net 支援多語言網站的實現與思考

來源:互聯網
上載者:User

      首先看一下我的方案總管中的檔案結構圖:

     

     在這裡,App_GlobalResources檔案是關鍵。它是ASP.NET 建立網站的資源檔類型的一種。

     有必要先介紹一下ASP.NET 的網站資源檔:

     “在 ASP.NET 中,可以建立具有不同範圍的資源檔。可以建立全域資源檔,這意味著可以從位於網站中的任意頁或代碼讀取這些資源檔。也可以建立本地資源檔,這些檔案儲存體單個 ASP.NET 網頁(.aspx 檔案)的資源。”

    全域資源檔

     將資源檔放入應用程式根目錄的保留檔案夾 App_GlobalResources 中,即可建立全域資源檔。App_GlobalResources 檔案夾中的任何 .resx 檔案都具有全域範圍。此外,ASP.NET 還會產生一個強型別對象,從而提供了一種以編程方式訪問全域資源的簡便方法。

   本地資源檔

     本地資源檔是只應用於一個 ASP.NET 頁或使用者控制項的檔案(副檔名為 .aspx、.ascx 或 .master 的 ASP.NET 檔案)。本地資源檔所放入的檔案夾具有 App_LocalResources 保留名稱。與根 App_GlobalResources 檔案夾不同,App_LocalResources 檔案夾可以位於應用程式的任意檔案夾中。通過使用資源檔的名稱,可以將一組資源檔與特定的網頁關聯起來。

例如,如果在 App_LocalResources 檔案夾中有一個名為 Default.aspx 的頁,則可以建立下列檔案:

  • Default.aspx.resx。這是未找到語言匹配項時的預設本地資源檔(回退資源檔)。

  • Default.aspx.es.resx。這是西班牙語的資源檔,其中不包含地區性資訊。

  • Default.aspx.es-mx.resx。這是專用於西班牙語(墨西哥)的資源檔。

  • Default.aspx.fr.resx。這是法語的資源檔,其中不包含地區性資訊。

檔案的基名稱與頁檔案名稱相同,後跟語言和地區性名稱,最後以副檔名 .resx 結尾。有關地區性名稱的列表,請參見 CultureInfo。

   CultureInfo

  

 

 

 

  

詳細參見MSDN:ms-help://MS.MSDNQTR.v90.chs/fxref_mscorlib/html/63c619e3-0969-2f01-a2d4-79d0868a98c6.htm

    也就是說圖中劃紅線的兩部分的命名,不能是自己隨便取的,必須要參考CultureInfo表來命名。

 

    WebResources.en.resx中的檔案資訊:

   

    WebResources.resx中的檔案資訊:

   

    Default.aspx的源設計代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>asp.net 支援多語言網站的實現方法</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="確認"/>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

    Default.aspx.cs的代碼:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace OneSiteMultiLanguage
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = Resources.WebResources.pagefile;
            Response.Write(Resources.WebResources.Question + "<br/>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int days = DateTime.Now.DayOfYear - Calendar1.SelectedDate.DayOfYear;
            if (days > 0)
            {
                Label1.Text = string.Format(Resources.WebResources.Answer, (365 - days).ToString());
            }
            else
            {
                Label1.Text = string.Format(Resources.WebResources.Answer, Math.Abs(days).ToString());
            }
            Button1.Text = string.Format(Resources.WebResources.Button1_Text);
        }
    }
}

    運行結果如下:

   

     注意在Default.aspx檔案中注意culture中的屬性值。

     還有若要讓瀏覽器以中文的語言顯示網頁內容時,可以在瀏覽器中的“工具”->”interner 選項“->“語言”進行設定。
    

     設定成中文後運行顯示:

    

——————《完》——————

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.