码迷,mamicode.com
首页 > 编程语言 > 详细

C# Winform软件多语言(汉语、英语。。。)界面的切换,低耦合

时间:2015-12-28 21:47:47      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

Winform软件多语言切换,个人见解,降低软件对语言展示的耦合度。

1、设计图(自己瞎画的呵呵)

技术分享

2、做的小demo,界面如下

技术分享技术分享

3、下面是代码展示部分

    1)Form1代码展示

 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类代码展示

 1 namespace WindowsFormsApplication1
 2 {
 3     public class GlobalData
 4     {
 5         /// <summary>
 6         /// 系统语言(Chinese(中文),English(英文)。。。)
 7         /// </summary>
 8         public static string SystemLanguage = System.Configuration.ConfigurationManager.AppSettings["Language"];
 9 
10         private static Language globalLanguage;
11         public static Language GlobalLanguage
12         {
13             get
14             {
15                 if (globalLanguage == null)
16                 {
17                     globalLanguage = new Language();
18                     return globalLanguage;
19                 }
20                 return globalLanguage;
21             }
22         }
23         
24     }
25 }

    3)Language类代码展示

 1 namespace WindowsFormsApplication1
 2 {
 3     public class Language
 4     {
 5         #region 登陆界面
 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         /// 读取XML放到内存
20         /// </summary>
21         /// <param name="language"></param>
22         protected void XmlLoad(string language)
23         {
24             try
25             {
26                 XmlDocument doc = new XmlDocument();
27                 string address = AppDomain.CurrentDomain.BaseDirectory + "Languages\\" + 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代码展示

1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <appSettings>
4     <!-- Language的值只能是Chinese(中文),English(英文) -->
5     <add key="Language" value="English"/>
6   </appSettings>
7 </configuration>

    5)English.xml代码展示

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)Chinese.xml代码展示

1 <?xml version="1.0" encoding="utf-8" ?>
2 <Chinese>
3   <Login_UserName>用户名:</Login_UserName>
4   <Login_UserPwd>密码:</Login_UserPwd>
5   <Login_Login>登录</Login_Login>
6 </Chinese>

4、如果想要换法语则只需要在App.config中配置换成French,然后再添加一个French.xml,内容和其他语言模板一样把内容换成法语则可。

5、OK。

C# Winform软件多语言(汉语、英语。。。)界面的切换,低耦合

标签:

原文地址:http://www.cnblogs.com/liangwenchao-912/p/5084076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!