Windows Forms Application multi-language support

Source: Internet
Author: User

Recently I have some ideas to study the application of languages that can be switched in real time and easily changed.Program, Search for some information on the Internet, and refer to the msdn documents to finally make a simple class for multi-language support.

Note: this idea and class refer to the article "Implementation of multiple languages in Windows Programming in C #" and I would like to express my gratitude to the author.
By the way, I despise the websites that are repeatedly reproduced, and even the author's name is changed !!!

The basic idea is relatively simple:

    1. When switching the language, call the corresponding XML resource to the hash table.
    2. Obtain all interface elements when modifying the interface
    3. Search for corresponding values from the hash table
    4. Assign a value.

The difficulty lies in obtaining all interface elements.

For Windows form applications, access form. Controls to obtain the controls contained in the form. Loop through all the controls and get their types, and then perform different processing on different types. Some controls can contain more controls, which means that you need to use a recursive call to traverse all controls and call the setsubcontrols subroutine in my program to do this.

I have also defined the message to be displayed in the XML file. Each message has an idnumber that identifies the content to be displayed based on different idnumbers. Each language has the same ID, but its content is different.

This method has the following advantages:

    1. In the XML file, you only need to provide the name of the form and control to facilitate editing and debugging.
    2. You can easily create more languages to meet your custom language needs without modifying the program.

Based on the above ideas, you can design a localization class that is responsible for language switching, including the following functions (for details, seeSource code)

Public Static Void Setlanguage ( String Lang) // set the global language
Public Static String Getmessage ( String ID) // get the corresponding message
Public Static Void Setform ( Form Form) // set language for Form
Private Static Void Setdropdownitems ( Toolstripitemcollection Items, Hashtable Table) // process the menu dropdown items
Private Static Void Setsubcontrols ( Control . Controlcollection Controls,Hashtable Table) // process Sub-Controls
Private Static Hashtable Readwindowresource ( String Frmname, String Lang) // read resources from an XML file
Private Static Void Readmessageresource () // read the message Resource
The above static functions constitute the basic of the class. You only need to setlanguage and then setform for calling.

Example XML file:

<? XML Version = " 1.0 " Encoding = " UTF-8 " ?>
< Resources >
< Form >
< Name > Frmmultilanguagedemo </ Name >
< Controls >
< Control Name = " Btnen " Text = " En " />
< Control Name = " Btnchn " Text = " CHN " />
< Control Name = " Txtcurrw.lang " Text = " English "/>
< Control Name = " Lbltext " Text = " Label " />
< Control Name = " Chkbox1 " Text = " Checkbox " />
< Control Name = " Tabpage1 " Text = " Page 1 " />
< Control Name = " Tabpage2 " Text = " Page 2 " />
< Control Name = " Radiobutton1 " Text = " Option "/>
< Control Name = " Btnshowmsg " Text = " Show message " />
< Control Name = " Btnshowwin " Text = " New window " />
< Control Name = " Mnufile " Text = " File " />
< Control Name = " Mnuexit " Text = " Exit " />
< Control Name = " Mnuhelp " Text = " Help "/>
< Control Name = " Mnuoptions " Text = " Options " />
< Control Name = " Mnuoption1 " Text = " Option 1 " />
</ Controls >
< Name > Frmnewwindow </ Name >
< Controls >
< Control Name = "Txttext " Text = " New window " />
</ Controls >
</ Form >
< Messages >
< Message ID = " Msg_test " Text = " Test message " />
</ Messages >
</ Resources >

Of course, this class is still relatively weak. The method for customizing the control loop traversal needs to be improved, and the storage structure of XML files needs to be further optimized, when you modify windows forms controls, you cannot synchronously update language resources.

Download source code and sample programs

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.