C # implementation of multiple languages in Windows Programming

Source: Internet
Author: User

You can use the configuration file to read resources from the configuration file, and then dynamically display the selected language in the load () method when the window is displayed.

The steps for implementing multiple languages in C # windows programming are as follows:

Step 1: Set the default language and modify the default language

First, an XML file is defined to store the default language. The file name is languagedefine. xml.

Languagedefine. xml

<? XML version = "1.0" standalone = "yes"?>
<Language>
<Defaultlanguage> ZH </defaultlanguage>
</Language>

This file has only one defaultlanguage attribute, which is used to store the default language. Note that this attribute must be the same as the _ character string in the language configuration file.

The following functions are used to read and modify the default language:

// Read the default language
Public static string readdefaultlanguage (){
Xmlreader reader = new xmltextreader ("Resources/languagedefine. xml ");
Xmldocument Doc = new xmldocument ();
Doc. Load (Reader );
Xmlnode root = Doc. documentelement;
// Select the defaultlangugae Node
Xmlnode node = root. selectsinglenode ("defaultlanguage ");

String result = "en ";
If (node! = NULL)
// Retrieve the content from the node
Result = node. innertext;

Reader. Close ();
Return result;
}
// Modify the default language
Public static void writedefaultlanguage (string Lang ){
Dataset DS = new dataset ();
DS. readxml ("Resources/languagedefine. xml ");
Datatable dt = Ds. Tables ["language"];

DT. Rows [0] ["defaultlanguage"] = Lang;
DS. acceptchanges ();
DS. writexml ("Resources/languagedefine. xml ");
}

We also use an XML file to store the types of languages to be displayed, and select the display text from the drop-down menu in a specific language. Now we only construct two languages, but by adding items, we can easily add a language function.

A ComboBox drop-down menu is used to select multiple languages. When the corresponding language is selected, the default language of the system, namely, languagedefine, is used. change the language in XML to the selected language. At the same time, the system immediately loads () to display the interface corresponding to the selected language.

Ppconfig. xml

<? XML version = "1.0" encoding = "gb2312"?>
<Appconfig>
<Area>
<Language> ZH </language>
<List>
<Item> Chinese [zh] </item>
<Item> English [En] </item>
</List>
</Area>
<Area>
<Language> en </language>
<List>
<Item> Chinese [zh] </item>
<Item> English [En] </item>
</List>
</Area>
<Area>
</Appconfig>

The function for reading the configuration file is:

Public static ilist getpolicagelist (string Lang ){
Ilist result = new arraylist ();

Xmlreader reader = new xmltextreader ("Resources/appconfig. xml ");
Xmldocument Doc = new xmldocument ();
Doc. Load (Reader );

Xmlnode root = Doc. documentelement;
Xmlnodelist nodelist = root. selectnodes ("area [Language = '" + Lang + "/LIST/item ");
Foreach (xmlnode node in nodelist ){
Result. Add (node. innertext );
}
Reader. Close ();

Return result;
}

Step 3: Obtain the display value of the page to be displayed based on the language

Through the preceding steps, we can obtain the Display language of the page based on the readdefaultlanguage () function in the load () method of each page. After obtaining the default language, we need to read the corresponding resources in the resource file of the language.

The format of the resource file is as follows (only English resource files are displayed. The format of Chinese resource files is the same, but the value in text is replaced with the corresponding Chinese value ):

Appresource_en.xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Resource>
<Form>
<Name> frmlogon </Name>
<Controls>
<Control name = "frmlogon" text = "User Logon"/>
<Control name = "lblusername" text = "username:"/>
<Control name = "lblpassword" text = "Password:"/>
<Control name = "lbllanguage" text = "language:"/>
<Control name = "btnlogon" text = "Logon"/>
<Control name = "btncancel" text = "exit"/>
<Control name = "btnchangepwd" text = "Change Password"/>
</Controls>
</Form>
<Form>
<Name> frmpasswordchange </Name>
<Controls>
<Control name = "frmpasswordchange" text = "Change Password"/>
<Control name = "lblusername" text = "username:"/>
<Control name = "lblpassword" text = "old password:"/>
<Control name = "lblnewpassword" text = "New Password:"/>
<Control name = "lblpasswordconfirm" text = "password confirm:"/>
<Control name = "btnok" text = "OK"/>
<Control name = "btncancel" text = "return"/>
</Controls>
</Form>
</Resource>

We put all the names of controls in a form for multi-language display in <controls> </controls>. name indicates the name attribute of the control, text refers to the text attribute of the control.

The read functions are as follows:

/// <Summary>
/// Read multi-language resource files
/// </Summary>
/// <Param name = "frmname"> name of the form </param>
/// <Param name = "Lang"> language (such as ZH or EN) to be displayed </param>
/// <Returns> </returns>
Public static hashtable readresource (string frmname, string Lang ){
Hashtable result = new hashtable ();

Xmlreader reader = NULL;
Fileinfo Fi = new fileinfo ("Resources/appresource _" + Lang + ". xml ");
If (! Fi. exists)
Reader = new xmltextreader ("Resources/appresource. xml ");
Else
Reader = new xmltextreader ("Resources/appresource _" + Lang + ". xml ");

Xmldocument Doc = new xmldocument ();
Doc. Load (Reader );

Xmlnode root = Doc. documentelement;
Xmlnodelist nodelist = root. selectnodes ("form [name = '" + frmname + "']/controls/control ");
Foreach (xmlnode node in nodelist ){
Try {
Xmlnode node1 = node. selectsinglenode ("@ name ");
Xmlnode node2 = node. selectsinglenode ("@ text ");
If (node1! = NULL ){
Result. Add (node1.innertext. tolower (), node2.innertext );
}
} Catch (formatexception Fe ){
Console. writeline (Fe. tostring ());
}
}
Reader. Close ();
Return result;
}

Step 4: display the corresponding value

We can call corresponding functions in the load () event of each form to implement this function. Of course, we must first obtain the code (such as en or ZH) of the language to be displayed in this form ), this value can be stored in a "global" variable (for example, the value is stored in global. setvalue ("Lang", langugae) is saved and stored through global. getvalue ("Lang ).

/// <Summary>
/// Obtain the control name
/// </Summary>
/// <Param name = "form"> </param>
Public static void getnames (Form)
{

// Obtain the display text of the table based on the language selected by the user
Hashtable table = appconfig. readresource (Form. Name, global. getvalue ("Lang"). tostring ());
Control. controlcollection controlnames = form. controls;
// You can set some unified properties of the form here, so that all forms will apply this setting.
// Form. keypreview = true;
// Form. maximizebox = false;
// Form. minimizebox = false;
// Form. controlbox = false;
// Form. formborderstyle = formborderstyle. fixeddialog;
// Form. startposition = formstartposition. centerscreen;
// Form. topmost = true;
// Form. keydown + = new keyeventhandler (onenter );
Try
{
Foreach (Control in controlnames)
{
If (control. GetType () = typeof (system. Windows. Forms. Panel ))
Getsubcontrols (control. Controls, table );

If (control. GetType () = typeof (system. Windows. Forms. groupbox ))
Getsubcontrols (control. Controls, table );

// If (control. GetType () = typeof (system. Windows. Forms. textbox) & control. enabled)
// Control. gotfocus + = new eventhandler (txt_focus );

If (table. Contains (control. Name. tolower ()))
Control. Text = (string) Table [control. Name. tolower ()];
}
If (table. Contains (Form. Name. tolower ()))
Form. Text = (string) Table [Form. Name. tolower ()];
}
Catch (exception ex)
Console. writeline (ex. tostring ());
}

Some controls have child controls, so we also need a function to read the display value of the child controls.

/// <Summary>
/// Obtain the display name of the child Control
/// </Summary>
/// <Param name = "controls"> </param>
/// <Param name = "table"> </param>
Private Static void getsubcontrols (control. controlcollection controls, hashtable table)
{
Foreach (Control in controls)
{
If (control. GetType () = typeof (system. Windows. Forms. Panel ))
Getsubcontrols (control. Controls, table );

If (control. GetType () = typeof (system. Windows. Forms. groupbox ))
Getsubcontrols (control. Controls, table );

If (table. Contains (control. Name. tolower ()))
Control. Text = (string) Table [control. Name. tolower ()];
}
}

Transfer http://dev.21tx.com/2006/09/11/11822.html

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.