[C# winform two ways to achieve multi-language switching]


Recently, when making a program, it is necessary to use software for multi-language versions. After collecting some information on the Internet, we are now implementing two file methods. The specific content is organized as follows, please refer to it.

xml file to realize Winform multilingual switching

Hello! This is the welcome page displayed for the first time you use the Markdown editor . If you want to learn how to use the Markdown editor, you can read this article carefully to understand the basic syntax of Markdown.

Logical Design Diagram

Logic block diagram

Code and configuration part display

1. Attribute configuration

The form property Localizable is set to TRUE, and Language selects the required language
Display of configuration settings

2.Form code display

        public Form2(Form1 form1)
        {
    
    
            InitializeComponent();
            SetLanguage();
        }


        private void SetLanguage()
        {
    
    
            this.button1.Text = GlobalData.GlobalLanguage.Login_UserName;
            this.button2.Text = GlobalData.GlobalLanguage.Login_UserPwd;
            this.button3.Text = GlobalData.GlobalLanguage.Login_Login;
        }

3.GlobalData class code display

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace MyNoteTool
{
    
    
    public class GlobalData
    {
    
    
        /// <summary>
        /// 系统语言(Chinese(中文),English(英文)。。。)
        /// </summary>
        public static string SystemLanguage = System.Configuration.ConfigurationManager.AppSettings["Language"];

        private static Language globalLanguage;
        public static Language GlobalLanguage
        {
    
    
            get
            {
    
    
                if (globalLanguage == null)
                {
    
    
                    globalLanguage = new Language();
                    return globalLanguage;
                }
                return globalLanguage;
            }
        }
    }
}

4.Language class code display

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MyNoteTool
{
    
    
    public class Language
    {
    
    
        #region 登陆界面
        public string Login_UserName = "";
        public string Login_UserPwd = "";
        public string Login_Login = "";
        #endregion

        protected Dictionary<string, string> DicLanguage = new Dictionary<string, string>();
        public Language()
        {
    
    
            XmlLoad(GlobalData.SystemLanguage);
            BindLanguageText();
        }

        /// <summary>
        /// 读取XML放到内存
        /// </summary>
        /// <param name="language"></param>
        protected void XmlLoad(string language)
        {
    
    
            try
            {
    
    
                XmlDocument doc = new XmlDocument();
                string address = AppDomain.CurrentDomain.BaseDirectory + "Languages\\" + language + ".xml";
                doc.Load(address);
                XmlElement root = doc.DocumentElement;

                XmlNodeList nodeLst1 = root.ChildNodes;
                foreach (XmlNode item in nodeLst1)
                {
    
    
                    DicLanguage.Add(item.Name, item.InnerText);
                }
            }
            catch (Exception ex)
            {
    
    
                throw ex;
            }
        }

        public void BindLanguageText()
        {
    
    
            Login_UserName = DicLanguage["Login_UserName"];
            Login_UserPwd = DicLanguage["Login_UserPwd"];
            Login_Login = DicLanguage["Login_Login"];
        }
    }
}

5. App.config code display

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<startup>
		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
	</startup>
	<appSettings>
		<!-- Language的值只能是Simple Chinese(中文),English(英文) -->
		<add key="Language" value="Simple Chinese"/>
	</appSettings>

</configuration>

6. English.xml code display

Note: The generated language folder is generated in the root directory of the project by default, and needs to be copied to the folder where the corresponding exe is generated!

<?xml version="1.0" encoding="utf-8" ?>
<English>
	<Login_UserName>Find</Login_UserName>
	<Login_UserPwd>Replace</Login_UserPwd>
	<Login_Login>All Replace</Login_Login>
</English>

7. Simple Chinese.xml code display

<?xml version="1.0" encoding="utf-8" ?> 
<Chinese>
 <Login_UserName>查找</Login_UserName>
  <Login_UserPwd>替换</Login_UserPwd>
   <Login_Login>全部替换</Login_Login>
 </Chinese>

Demo effect display

display 1
show 2

Resx file realizes Winform multilingual switching

Form property settings

1. Set the Localizable property of Form to true (after setting this property, .net will generate different resource files for the application according to different languages), and then set the Language property to the required language, as shown below.
configuration
2. After setting the Language property to English, for example, then we adjust the names of the controls in the form, as shown in Figure
1
3. This is also the case for other languages, just take English as an example.
insert image description here

4. After we have adjusted the page, we regenerate it, and then switch the above Language property back to the default or Chinese, and then regenerate the project. In this case, the language file will be automatically generated.
insert image description here
Note: Be sure to restart it, otherwise the language file will not appear if you run it directly after changing it!

Add the variable DefaultLanguage to the Settings.settings of the project's Properties to save the default language of the current setting. The next time the program is started, this variable is read, thereby setting the language of the program to the language of the last time the program was closed. Set the default language to Chinese zh-CN for the first time.

Guess you like

Origin blog.csdn.net/weixin_37864926/article/details/131222194
Recommended