C # Winform software multi-language (Chinese, English ...) Interface switching, low coupling,
Winform software supports multi-language switching and reduces the software's coupling to language display.
1. Design Drawing (self-painted)
2. Make a demo with the following interface:
3. The following section shows the code.
1) Form1 code Display
1 namespace WindowsFormsApplication1 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 SetLanguage(); 9 }10 11 private void SetLanguage()12 {13 this.lbUserName.Text = GlobalData.GlobalLanguage.Login_UserName;14 this.lbPwd.Text = GlobalData.GlobalLanguage.Login_UserPwd;15 this.btnLogin.Text = GlobalData.GlobalLanguage.Login_Login;16 }17 }18 }
2) GlobalData class code Display
1 namespace WindowsFormsApplication1 2 {3 public class GlobalData 4 {5 // <summary> 6 // System Language (Chinese), English (English )...) 7 /// </summary> 8 public static string SystemLanguage = System. configuration. configurationManager. appSettings ["Language"]; 9 10 private static Language globalLanguage; 11 public static Language GlobalLanguage12 {13 get14 {15 if (globalLanguage = null) 16 {17 globalLanguage = new Language (); 18 return globalLanguage; 19} 20 return globalLanguage; 21} 22} 23 24} 25}
3) Language code Display
1 namespace WindowsFormsApplication1 2 {3 public class Language 4 {5 # region logon interface 6 public string Login_UserName = ""; 7 public string Login_UserPwd = ""; 8 public string Login_Login = ""; 9 # endregion 10 11 protected Dictionary <string, string> DicLanguage = new Dictionary <string, string> (); 12 public Language () 13 {14 XmlLoad (GlobalData. systemLanguage); 15 BindLanguageText (); 16} 17 18 /// <summary> 19 // read XML and put it in memory 20 /// </summary> 21 /// <param name = "language"> </ param> 22 protected void XmlLoad (string language) 23 {24 try25 {26 XmlDocument doc = new XmlDocument (); 27 string address = AppDomain. currentDomain. baseDirectory + "ages \" + language + ". xml "; 28 doc. load (address); 29 XmlElement root = doc. documentElement; 30 31 XmlNodeList nodeLst1 = root. childNodes; 32 foreach (XmlNode item in nodeLst1) 33 {34 DicLanguage. add (item. name, item. innerText); 35} 36} 37 catch (Exception ex) 38 {39 throw ex; 40} 41} 42 43 public void BindLanguageText () 44 {45 Login_UserName = DicLanguage ["Login_UserName"]; 46 Login_UserPwd = DicLanguage ["Login_UserPwd"]; 47 Login_Login = DicLanguage ["Login_Login"]; 48} 49} 50}
4) App. config code Display
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <appSettings> 4 <! -- The value of Language can only be Chinese or English) --> 5 <add key = "Language" value = "English"/> 6 </appSettings> 7 </configuration>
5) English. xml code Display
1 <?xml version="1.0" encoding="utf-8" ?>2 <English>3 <Login_UserName>UserName:</Login_UserName>4 <Login_UserPwd>Password:</Login_UserPwd>5 <Login_Login>Login</Login_Login>6 </English>
6) display Chinese. xml Code
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <Chinese> 3 <Login_UserName> User name: </Login_UserName> 4 <Login_UserPwd> password: </Login_UserPwd> 5 <Login_Login> logon </Login_Login> 6 </Chinese>
4. If you want to change to French, you only need to configure to change to French in App. config, and then add a French. xml file. The content can be changed to French like other language templates.
5. OK.