多语言系统如何实现

多语言系统如何实现

一、总结

一句话总结:

1、如果系统只是简繁体,怎么实现?

直接就可以用函数实现了. 因为他们具有一一对应关系

2、系统简体、繁体、英文三个版本切换,用什么方法实现?

xml

<Labels>
 <Label>
  <id>User</id>
  <name>用户</name>
 </Label>
 <Label>
  <id>Password</id>
  <name>密码</name>
 </Label>

到时候就让用户选择不同的版本加载不同的就可以了

 private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.English.HRef = "public/login.aspx?Version=eng";
                this.ChineseT.HRef = "public/login.aspx?Version=cht";
                this.ChineseS.HRef = "public/login.aspx?Version=chs";

二、多语言系统的实现

被老美卖掉后, 工作多了,工资涨得却少了,做不完的活, 现总算完成了手头上的紧急工作,上来写一下有关多语言系统的实现, 我们的做法是:如果系统只是简繁体,直接就可以用函数实现了. 因为他们具有一一对应关系,可是其它语言呢? 由于不具有语言的对照关系,只能分别写了. 最初的系统我们是采用写多个页面来实现,后面觉得这种方法不但工作量大,而且改功能的话,同一事情要重复劳动.后面我们就采用XML记录的方式来处理了. 不知各位大侠是如何做的呢?

 private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.English.HRef = "public/login.aspx?Version=eng";
                this.ChineseT.HRef = "public/login.aspx?Version=cht";
                this.ChineseS.HRef = "public/login.aspx?Version=chs";
               
            }
        }

系统默认做成的是英文的. 如果上面选择是简体中文,则会判断,并加载相应语言的XML

 private void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.lblError.Text = "";
            if (!this.Page.IsPostBack)
            {
                if (base.Request.QueryString.Get("Version") != null)
                {
                    this.Session["Version"] = base.Request.QueryString.Get("Version");
                }
                else
                {
                    this.Session["Version"] = "chs";
                }
                if (this.Context.User.Identity.IsAuthenticated)
                {
                    base.Response.Redirect("../mainform.aspx");
                }
                switch (this.Session["Version"].ToString())
                {
                    case "chs":
                        base.SetControlsText(base.Server.MapPath("login_chs.xml"));
                        break;

                    case "cht":
                        base.SetControlsText(base.Server.MapPath("login_cht.xml"));
                        break;
                }
                this.btnLogin.Attributes.Add("onclick", "return checkuser();");
                this.AddAdmin();
            }
            this.CheckUser();
        }

上面的函数SetControlsText定义, 就是把相应的菜单替换成你想要的语言.

 protected void SetControlsText(string Filename)
        {
            XmlDocument document = new XmlDocument();
            document.Load(Filename);
            XmlNode node = document.SelectSingleNode("/Controls");
            this.SetLabelText(node);
            this.SetButtonText(node);
            this.SetCheckBoxText(node);
            this.SetRadioButtonText(node);
            this.SetRadioButtonListText(node);
            this.SetDataGridHeaderText(node);
            this.SetCusDataGridHeaderText(node);
        }

 xml文件里定义的内容是:

<?xml version="1.0" encoding="gb2312"?> 
<Controls>
<Labels>
 <Label>
  <id>User</id>
  <name>用户</name>
 </Label>
 <Label>
  <id>Password</id>
  <name>密码</name>
 </Label>
 <Label>
  <id>Forget your password?</id>
  <name>忘记密码了吗?</name>
 </Label>
 <Label>
  <id>Login</id>
  <name>登录</name>
 </Label>
</Labels>

<Buttons>
 <Button>
  <id>Login</id>
  <name>登录</name>
 </Button>
 <Button>
  <id>Home</id>
  <name>首页</name>
 </Button>
 <Button>
  <id>Email password</id>
  <name>发送密码到邮箱</name>
 </Button>
   <Button>
  <id>Send Mail to me</id>
  <name>发送密码到邮箱</name>
 </Button>
</Buttons>
<CheckBoxs>
 <CheckBox>
  <id>Login automatically</id>
  <name>自动登录</name>
 </CheckBox>
</CheckBoxs>
</Controls>

  

结果如上图. 其它页面亦如此.

 

 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/9302394.html