被老美賣掉后, 工作多了,工資漲得卻少了,做不完的活, 現總算完成了手頭上的緊急工作,上來寫一下有關多語言系統的實現, 我們的做法是:如果系統只是簡繁體,直接就可以用函數實現了. 因為他們具有一一對應關系,可是其它語言呢? 由於不具有語言的對照關系,只能分別寫了. 最初的系統我們是采用寫多個頁面來實現,后面覺得這種方法不但工作量大,而且改功能的話,同一事情要重復勞動.后面我們就采用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>

結果如上圖. 其它頁面亦如此.
