被老美卖掉后, 工作多了,工资涨得却少了,做不完的活, 现总算完成了手头上的紧急工作,上来写一下有关多语言系统的实现, 我们的做法是:如果系统只是简繁体,直接就可以用函数实现了. 因为他们具有一一对应关系,可是其它语言呢? 由于不具有语言的对照关系,只能分别写了. 最初的系统我们是采用写多个页面来实现,后面觉得这种方法不但工作量大,而且改功能的话,同一事情要重复劳动.后面我们就采用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文件里定义的内容是:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<?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> |

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

浙公网安备 33010602011771号