C # programming to implement multi-language WinForm program __ Programming

Source: Internet
Author: User
Tags static class

Read some on the internet, You also build the. resources file yourself, and then load it dynamically, this method does not try, it is also very troublesome, originally vs can easily generate. resx resource files, and then compile the DLL together for a long time did not figure out how to read each string separately, but also through other methods to achieve multi-language support, Only one by one regret is the need to restart the program, in fact, this is acceptable in many cases, as soon as the method of effective immediately after the study.

First, generate the resource file.
1. Set the form's property localizable to True
2. Set the form's property language to default, which is used to automatically load defaults through the UI language settings of the operating system, and if the language you set is not found, it defaults to Chinese (may default to English better), sets the properties of each control and fills in the complete.
3. Place the form's language into Chinese (United States)-en-US or other languages, modify the strings on each control, and when you change the properties of a control, VS automatically generates a resource file formname.en-us.resx.

This allows you to edit each language's resource file separately, and when you choose Language vs will load the corresponding language interface for you, you can also modify it. The language attribute was intended to be modified to display a different language interface, but there is no such attribute in the form class, which should be a property added to vs at design time.

Then, when the program starts, the language setting is loaded, and the core code is:

  
static void Main ()
{
	if (WordCracker.Config.ConfigManager.Language!= "default") {
		System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo ( WordCracker.Config.ConfigManager.Language);
	}
	Application.enablevisualstyles ();
	Application.setcompatibletextrenderingdefault (false);
	Application.Run (New MainForm ());
}
Among them, WordCracker.Config.ConfigManager is a class that I write an operation XML configuration file, the code is as follows:
 namespace Wordcracker.config {static class ConfigManager {private static XmlDocument doc = new XmlDocument ();
		private static FileInfo file = new FileInfo ("Config.xml"); Static ConfigManager () {if (!file).
			Exists) return; FileStream fs = new FileStream (file.
			Name, FileMode.Open); Doc.
			Load (FS); Fs.
		Close (); public static string Language {get {XmlNode lan = doc.
				selectSingleNode ("/configuration/ui/language");
				if (LAN = NULL) return "default"; else return LAN.
			InnerText; The set {XmlNode lan = doc.
				selectSingleNode ("/configuration/ui/language");
				if (LAN = NULL) {//Create node return; } LAN.
				innertext = value; if (file. Exists) file.
				Delete (); Doc. Save (file.
			Name); The configuration file config.xml is placed in the same directory as the program: 
<?xml version= "1.0" encoding= "Utf-8"

   
& nbsp;       en-US
   
Of course, here you can add other configuration information, is a small software, I did not add too many things

Here is the implementation of the start of the detection configuration file set in the language, and load the language interface, but has not changed the language function, modify the language of the operation written in a menu, a display in Chinese a display in English, the code is as follows:

private void Menuoptionslanguagechinese_click (object sender, EventArgs e)
{
	WordCracker.Config.ConfigManager.Language = "ZH-CN";
	Promptrestart ();			
}

private void Menuoptionslanguageenglish_click (object sender, EventArgs e)
{
	WordCracker.Config.ConfigManager.Language = "en-US";
	Promptrestart ();
}
private bool Promptrestart ()
{
	if (MessageBox.Show ("If you want to Chang language, the program need restart, Wou LD You restart now? ",
		" restart needed ",
		 Messageboxbuttons.yesno,
		 messageboxicon.warning) = = Dialogresult.yes) {
		System.Windows.Forms.Application.Restart ();
		return true;
	}
	return false;
}

Here is another problem, the pop-up prompts to restart the message is in Chinese or English, it is best to write to the configuration file, and detect language settings, set up why the language to display what language prompts message.

The disadvantage of this method is that it does not take effect immediately, if you want to take effect immediately, you need to manually add resources, you need to manually added a lot of things if necessary to do so.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.