[轉thyking xiao's blog]asp.net 2.0實現多語言(一)

來源:互聯網
上載者:User
1.建立一個asp.net web應用程式

2.建立的項目如所示

3.右擊web項目名稱,添加一個全域資源檔夾"app_GlobalResources" ,這個是asp.net 2.0特有的

4.右擊"app_GlobalResources"檔案夾,添加兩個資源檔: language.resx(簡體資源檔)  和language.en-us.resx (英文的資源檔)

5.開啟兩個資源檔,添加相應的資源資訊,如所示

6.開啟default.aspx檔案,輸入如下代碼:
<body>
    <form id="form1" runat="server">
    <center>
        <div style="margin: 20px; padding: 10px; height: 200px; width: 200px; border: solid 1px #C0C0C0;
            text-align: center;">
            <br />
            <a href="?curlanguage=zh-cn">中文</a> &nbsp;
             <a href="?curlanguage=en-us">英文</a>
            <br />
            <br />
            國家:&nbsp;<asp:Literal ID="ltlcountry" runat="server"></asp:Literal>
            <br />
            城市:&nbsp;<asp:Literal ID="ltlcity" runat="server"></asp:Literal>
           
             <br />
            <br />
            國家2:&nbsp;<asp:Literal ID="ltlcountry2" runat="server"></asp:Literal>
            <br />
            城市2:&nbsp;<asp:Literal ID="ltlcity2" runat="server"></asp:Literal>
        </div>
    </center>
    </form>
</body>

預覽如所示:

7.開啟Default.aspx.cs檔案,輸入如下代碼:

  

//這段代碼很重要 
protected override void InitializeCulture()
        {
            string culture = Request.QueryString["curlanguage"];
            if (!String.IsNullOrEmpty(culture))
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(culture);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ltlcountry.Text = Resources.language.country.ToString();
                ltlcity.Text = Resources.language.city.ToString();

                ltlcountry2.Text =(string)GetGlobalResourceObject("language", "country");
                ltlcity2.Text=(string)GetGlobalResourceObject("language", "city");

            }
        }

8. build下整個項目,按F5瀏覽: 點擊中的  "中文" 和 "英文" 連結就可以查看我們所要的效果了

聯繫我們

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