关闭

net2.0+xml实现多语言开发333

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

 method :

1.Index.aspx

 <form id="form1" runat="server">
    <div>
        <asp:Label ID="lanuage" runat="server">Label</asp:Label>
        <asp:DropDownList ID="drp_language" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drp_language_SelectedIndexChanged"
            Width="152px">
        </asp:DropDownList>
        <asp:Button ID="btnLogon" runat="server" Text="Button" OnClick="btnLogon_Click" /></div>
    </form>

2.index.aspx.cs

public partial class index : System.Web.UI.Page
    {
        public static string strName;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                init();
            }

        }
        private void init()
        {
            drp_language.DataSource = TangramMiniWeb.ResourceManager.GetSupportedLanguages();
            drp_language.DataTextField = "name";
            drp_language.DataValueField = "key";
            drp_language.DataBind();

            if (drp_language.Items.Count > 0)
            {
                strName = drp_language.SelectedValue;
                ShowText(drp_language.Items[0].Value);
            }
        }
        private void ShowText(string userlanguage)
        {
            btnLogon.Text = TangramMiniWeb.ResourceManager.GetString("GO", userlanguage);
            //strName = btnLogon.Text = MultLanuage.ResourceManager.GetString("btnLogon", userlanguage);
            lanuage.Text = TangramMiniWeb.ResourceManager.GetString("lanuage", userlanguage);

        }
        protected void drp_language_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowText(drp_language.SelectedValue);
            strName = drp_language.SelectedValue;
        }
        protected void btnLogon_Click(object sender, EventArgs e)
        {
            Session["selectvalue"] = drp_language.SelectedValue.ToString();
            Response.Redirect("tan/Default.aspx");
        }
    }

3.基类的代码   ResourceManager.cs

public class ResourceManager
 {
  
  public ResourceManager()
  {
            //
   
  }
  public static string GetString(string name,string userLanguages)
  {
            //通过指定属性名返回包含此属性名相关信息的hash表
   Hashtable source=LoadSorce(userLanguages);
            if (source != null)
            {
                //不为空就返回其值
                return source[name].ToString();
            }
            else
            {
                //为空
                return "";
            }

  }
          //获取语言目录数据,返回一个数据视图
  public static DataView GetSupportedLanguages()
  {
   string cacheKey = "Languages"; 
         //判断当前数据缓存中是否为空
   if(HttpContext.Current.Cache[cacheKey]==null)
   {
                //获取xml文件存放的物理路径
    string PathFile=System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath+"/Languages/languages.xml");
    DataSet objDataSet=new DataSet();
                //
    objDataSet.ReadXml(PathFile);
    CacheDependency dp = new CacheDependency(PathFile);
    HttpContext.Current.Cache.Insert(cacheKey,objDataSet.Tables[0].DefaultView,dp,DateTime.MaxValue, TimeSpan.Zero);
             }
    return (DataView)HttpContext.Current.Cache[cacheKey];

         }
  //获取语言语句字典

  public  static Hashtable LoadSorce(string userLanguages)
  {
            //获取xml文件存放的物理路径
   string PathFile=System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath+"/Languages/"+userLanguages+"/Resources.xml");
   //建一个XML DOCUMENT的实例对象
            System.Xml.XmlDocument LagXml=new XmlDocument();
            //导入要操作的XML文件
   LagXml.Load(PathFile);
            //
   Hashtable supportedLanguages;
            //建立HASHTABLE的对象实例
   supportedLanguages = new Hashtable();
   //遍历XML文件中的所有子节点
   foreach (XmlNode n in LagXml.SelectSingleNode("root").ChildNodes)
   {
    if (n.NodeType != XmlNodeType.Comment)
    {
                    //把子节点的键/值对添加到hashtable中
     supportedLanguages.Add(n.Attributes["name"].Value,n.InnerText);
    }

   }
            //返回hashtable
   return  supportedLanguages;
   
   
  }
 }

4.配制web.config

<!--  全球化
          此节设置应用程序的全球化设置。
    -->
  <globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
  <xhtmlConformance mode="Legacy"/> 

5.global.asax

global.asax.cs

global.asax.resx

6.建立资源文件

建个文件夹为Languages

再建三个数据文件  en-us  resourcesEN-US.xml

                                 zh-cn    resourcesZH-CN.xml

                                zh-tw   resourcesZH-TW.xml

                                (share xml files)  Language.xml 

 

 

这个小测试是成功的。 
我现在要把全站都用中文和英文业显示,那方法为 
就拿一个about.aspx这个网页来说吧,我说我的个人方法 
先作完about.aspx的中文页面,然后调用我那个语言的基类,再写这个about.aspx的XML的2种语言的XML的文件吗。  

最后每一个页面都要这样完成.

yaqi

                                                                                                                                     

 

 

0
0

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