关闭

net2.0的资源文件实现多语言开发! 2222

1357人阅读 评论(0) 收藏 举报

mfethod:

1.先作个 SElect Language.aspx

<form id="form1" runat="server">
    <div>
     
        <asp:Button ID="Button1" runat="server" meta:resourcekey="Button1Resource1" Text="Button" />
        <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" meta:resourcekey="Label1Resource1" Text="Label"></asp:Label></div>
          <asp:Button ID="Button2" runat="server" Text="<%$resources: global, String1%>" OnClick="Button2_Click" />

    </form>

 

2.selectLanguage.aspx.cs:

public partial class SelectLanguage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Button2.Text = Resources.global.String1;
            }

        }

 

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["PreferredCulture"] == "en-us")
            {
                Session["PreferredCulture"] = "zh-cn";

            }
            else if(Session["PreferredCulture"]=="en-us")
            {
                Session["PreferredCulture"] = "en-us";
           
            }

            Response.Redirect(Request.Url.PathAndQuery);

        }
    
}

 

3.写基类ResourceManager.cs

 

public class ResourceManager
    {
        public ResourceManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static string GetString(string name, string userLanguages)
        {
            //通过指定属性名返回包含此属性名相关信息的hash表
            Hashtable source = LoadSource(userLanguages);

            //如果hash表不等于null就将其值返回
            if (source != null)
            {
                return source[name].ToString();
            }
            else
            {
                return "";
              
            }

           
          
        }

        //获取语言语句字典
        public static Hashtable LoadSource(string userLanguages)
        {
            //获取xml文件存放的物理路径
            string pathFile = System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Languages/" + userLanguages + "/Resources.xml");

            //创建一个xmldatadocument对象
            System.Xml.XmlDataDocument lagXml = new XmlDataDocument();

            //加载其要操作的xml文件
            lagXml.Load(pathFile);

            //声明一个hash表
            Hashtable supportedLanguages;

            //创建hash表
            supportedLanguages = new Hashtable();

            //获取xml文件中root节点下的所有子节点
            foreach (XmlNode xn in lagXml.SelectSingleNode("root").ChildNodes)
            {
                //如果不是注释就将子节点属性值和节点文本以键/值对形式存放到hash表里
                if (xn.NodeType != XmlNodeType.Comment)
                {
                    supportedLanguages.Add(xn.Attributes["name"].Value, xn.InnerText);
                }
            }

           
            return supportedLanguages;
        }

 

    }

 

4.写配制Web.Config

<system.web>

  <anonymousIdentification enabled="true"/>
  <profile>
   <properties>
    <add name="LanguagePreference" type="string"  defaultValue="auto" allowAnonymous="true" />
   </properties>
  </profile>
    </system.web>

 

5.global.asax

<%@ Application Codebehind="Global.asax.cs" Inherits=" TangramMiniWeb.Global" %>

global.asax.cs

protected void Application_BeginRequest(Object sender, EventArgs e)
  {
            //CultureInfo类表示有关特定区域性的信息,提供信息的区域性特定对象的访问
            ultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
            //CurrentCulture属性用来获取或设置当前线程的区域性
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            //CurrentUICulture属性用来获取或设置资源管理器使用的当前区域性以便在运行时查找区域性特定的资源
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
  }

 

6.建立页面的资源文件:

 global.resx

global.en-us.resx

selectlanguage.resx

selectlanguage.en-us.resx

7.其他页面的继承的东西

比如:ablut.aspx.cs

 public partial class tan_About :ResourceManager
    {
        public void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               
               //
            }
          

        }
      
    }

 

 

最后就是作资源文件了:

 ablut..aspx.resx                                              // add chinese value

ablut.aspx.en-us.resx                                    //add english value

 

thank you

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75820次
    • 积分:1170
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:0篇
    • 译文:0篇
    • 评论:10条
    最新评论