代码改变世界

ASP.NET 下利用资源文件实现国际化

2012-08-22 00:03  音乐让我说  阅读(277)  评论(0编辑  收藏  举报

项目解决方案截图:

 

Resource 文件:

 

 

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 class="Title">
        <asp:Label ID="lbTitle" runat="server"></asp:Label>
    </div>
        <br />
    <div class="Author">
        <asp:Label ID="lbAuthor" runat="server" ></asp:Label>
    </div>
        <br />
    <div class="Content">
    <p>
        <%=strContent %>
    </p>
    <p><%=strLink %>: <a href="<%=strUrl %>"><%=strUrl %></a></p>
    </div>
<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True" 
        onselectedindexchanged="ddlLanguage_SelectedIndexChanged">
        </asp:DropDownList>
    </form>
</body>
</html>

aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Resources;
using System.Globalization;
using CSASPNETGloablizationInAssemblyResource;

namespace CSASPNETGloablizationInAssembly
{
    public partial class Default : System.Web.UI.Page
    {
        public string strContent = string.Empty;
        public string strUrl = string.Empty;
        public string strLink = string.Empty;
        const string strBaseName = "CSASPNETGloablizationInAssemblyResource.LanguageResource";

        ResourceManager manager = new ResourceManager(strBaseName, typeof(LanguageResource).Assembly);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CultureInfo culture = new CultureInfo(Context.Request.UserLanguages[0]);

                string strTitle = manager.GetString("Title", culture);
                string strAuthor = manager.GetString("Author", culture);
                this.strContent = manager.GetString("Content", culture);
                this.strUrl = manager.GetString("Url", culture);
                this.strLink = manager.GetString("Link", culture);
                lbTitle.Text = strTitle;
                lbAuthor.Text = strAuthor;
                bool flag = false;

                for (int i = 0; i < ddlLanguage.Items.Count; i++)
                {
                    if (ddlLanguage.Items[i].Value == culture.Name.ToLower())
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    ddlLanguage.SelectedValue = culture.Name.ToLower();
                }
                else
                {
                    ddlLanguage.SelectedIndex = 0;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ddlLanguage.Items.Add(new ListItem("United State", "en-us"));
            ddlLanguage.Items.Add(new ListItem("France", "fr-fr"));
            ddlLanguage.Items.Add(new ListItem("中国", "zh-cn"));
        }

        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            string languageCode = ddlLanguage.SelectedValue;
            CultureInfo currentCulture = this.GetLanguageSpecifically(languageCode);
            lbTitle.Text = manager.GetString("Title", currentCulture);
            lbAuthor.Text = manager.GetString("Author", currentCulture);
            this.strContent = manager.GetString("Content", currentCulture);
            this.strLink = manager.GetString("Link", currentCulture);
            this.strUrl = manager.GetString("Url", currentCulture);
        }

        public CultureInfo GetLanguageSpecifically(string languageCode)
        {
            CultureInfo culture = new CultureInfo(languageCode);
            return culture;
        }
    }
}

运行效果图:

 

谢谢浏览!