How to develop a multi-language C # Program

Source: Internet
Author: User
There are multiple methods to use resource files in C #, which can easily develop globally-used applications. Program . This article uses a simple example to explain how to use resource files in C # to quickly develop a multi-language program. In this example, there is only one form and three controls, which can be run on a Chinese or English interface based on user settings. The development environment is Windows 2000 Professional and Microsoft Visual Studio. NET 2002. The development procedure is as follows:

1. Create a project

Start Microsoft Visual Studio. NET and create a C # project MyApp. This project is stored in F: \ CSHARP \ MyApp.

2. Add controls

Add three controls on the form1 form (see figure 1): Main Menu mainmenu, label label1, And button button1. The label1 and button1 controls use the default settings. Add three menu items menuitem1, menuitem2, and menuitem3 to the mainmenu control. The text attribute is set to "language", "Chinese", and "English ". Menuitem2,
Set the radiocheck attribute of menuitem3 to true.


Figure 1

3. Create a configuration file

Add a text file myappconfig. ini to the project to save the language version settings of the current program. This file contains only one line of content. If it is "English", the program is in English; if it is "Chinese", it is in Chinese. It is now saved as "English ".

4. Create a resource file

There are three types of resource files in. Net: The. resx file in XML format, and the. Resources file. A text file can only contain string Resources in the format of "name = value .. The resx and. Resources files not only save string resources, but also save serialized Persistent Object resources such as images. However, only resource files in the. Resources file format can be embedded into the executable files of the Runtime library or compiled as affiliated assemblies. Use the resource file generator in Visual Studio. NET
(Resgen.exe), you can convert the. txt file and. resx file to the. Resources file format. In this example, only the serial resources are used. The two sequence files resource_zh.txt and reource_en.txt are used in the project.

Content in reource_en.txt:

Menuitem1_name = Language
Button1_name = exit
Labelemeditext = This is English version!

Content in resource_zh.txt:

Menuitem1_name = Language
Button1_name = exit
Labelemeditext = This is the Chinese version!

Resource_zh.txt file contains Chinese characters, must be saved in UTF-8 encoding. In the Visual Studio. NET integrated development environment, you can first click "Advanced save options" in the "Files" menu, select the UTF-8 encoding format, and then save the file.

Then, click "Visual Studio. net command prompt "menu, enter the DOS mode, enter the following two lines of command, generate the resource file MyApp that can be accessed in the program. resources and MyApp. zh-CN.resources. MyApp. Resources is the default backup resource file. MyApp. zh-CN.resources
Is a Chinese resource file. The "MyApp" in the file name is the root name of the resource file. It must be the same as the root name of the backup resource file. "ZH-CN" specifies the resource file culture.

F: \ CSHARP \ MyApp> resgen resource_en.txt, MyApp. Resources
F: \ CSHARP \ MyApp> resgen resource_zh.txt, MyApp. ZH-CN.
Resources

5. ModifyCode

1. Add the namespace to be referenced at the beginning of the Code.

Using system. Globalization;
Using system. Resources;
Using system. Threading;
Using system. IO;

2. Define two fields Ci and RM in form1. The cultureinfo object Ci is used to specify the culture when the resource file is read. The ResourceManager object RM is used to read resources from the specified resource file.

Cultureinfo CI = new cultureinfo ("");
Res ource manag er Rm = res ource manag er. createfilebasedresourcemanager ("MyApp", ".", null );

3. Add a method readresources in form1 to read the string Resources in the corresponding resource file and modify the text display on the interface menu, tag, and button.

Private void readresources (){
This. menuitem1.text = RM. getstring ("menuitemshorttext", CI );
This. button1.text = RM. getstring ("button1_text", CI );
This. label1.text = RM. getstring ("label1_text", CI );
This. Refresh ();
}

4. During the load event processing of form1, The readresources method is called based on the content of the configuration file to read resources and display the program interface.

private void form1_load (...) {
streamreader sr = file. opentext ("myappconfig. ini ");
If (Sr. readline () = "Chinese") {
Ci = new cultureinfo ("ZH-CN");
menuitem2.checked = true ;}
else {
Ci = new cultureinfo ("en");
menuitem3.checked = true;
}< br> Sr. close ();
This. readresources ();
}

5. Add the saveconfig method to form1 to modify the configuration file. Call the s a v e c o n f I g method during the click events of menuitem2 and menuitem3, respectively, and write the strings "Chinese" and "English" into the configuration file, call the readresources method to update the interface.

Private void saveconfig (string p_language ){
Streamwriter Sw = file. createtext ("myappconfig. ini ");
Sw. writeline (p_language );
Sw. Close ();
}
Private void menuitem2_click (...){
This. saveconfig ("Chinese"); CI = new cultureinfo ("ZH-CN ");
Menuitem2.checked = true; menuitem3.checked = false;
This. readresources ();
}
Private void menuitem3_click (...){
This. saveconfig ("English ");
Ci = new cultureinfo ("en ");
Menuitem2.checked = false;
Menuitem3.checked = true;
This. readresources ();
}

6. Close the form and end the running program during the Click Event of the button button1.

Private void button#click (...)
{
This. Close ();
}

6. Compile and run the program

If you manually compile the source program, you only need to click the "Visual Studio. NET" command to enter the Chinese menu, enter the following command, and then generate the binary executable file myapp.exe. Then enter MyApp to run the program.

F: \ CSHARP \ MyApp> CSC/out: myapp.exe form1.cs

If you run a program in the Microsoft Visual Studio. NET integrated development environment, the program is automatically compiled and the compiled binary executable file is placed in the bin \ debug subdirectory. At this point, you need to note that the myappconfig. ini, MyApp. Resources and MyApp. zh-CN.resources files are also copied to the bin \ debug \ subdirectory, the program can run properly.

When the program runs for the first time, it is an English interface (see figure 2). Click "Chinese" in the menu to turn it into a Chinese interface (see figure 3 ). Click "exit" to exit the program. When you run the program again, the Chinese interface is displayed.


Figure 2

Figure 3

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.