How to develop a multiple language version of a C # program

Source: Internet
Author: User
Tags exit ini interface modify net command resource visual studio
Programs in C # have a variety of ways to use resource files, and can easily develop global applications. This article is a simple example of how to use a resource file in C # to quickly develop a multiple language version of the program. In this case, there is only one form, three controls that can be run in either Chinese or English according to the user's settings. The development environment is Windows Professional, Microsoft Visual Studio.NET 2002. The specific development steps are as follows:





One, new project





starts Microsoft Visual Studio. NET, creates a new C # project MYAPP, and this example works in F:\CSHARP\MYAPP.





Two, add controls





adds three controls to the Form1 form (see Figure 1): Main Menu MainMenu, label Label1, and Button Button1. The properties of the Label1 and Button1 controls all take the default setting. Add three menu items to the MainMenu control menuItem1, MENUITEM2, MenuItem3. The Text property is set to "language", "Chinese", and "中文版" respectively. MENUITEM2,

The RadioCheck property of the
MenuItem3 is set to true.








Figure 1


Three, create a configuration file





adds a text file Myappconfig.ini to the project, saving the language version settings for the current program. There is only one line of content in the file. If it is "Chinese", then the program is in English, and if it is "Chinese", it is the language version. This is saved as "中文版" for the moment.





Four, create a resource file





  . NET has three kinds of resource files: text (. txt) file, XML format. Resx files and. Resources file. A text file can contain only string resources in the form of a name = value format ... Both the ResX file and the. resources file can hold not only the string resource, but also the serializable persistent object resources such as images. However, only resource files in the. resources file format can be embedded in the runtime executable file or compiled into satellite assemblies. Take advantage of your own resource file Builder in Visual Studio. NET


(Resgen.exe), you can convert. txt files and. resx files to the. resources file format. In this example, only the string resource is used, so two text files Resource_zh.txt and Reource_en.txt are added to the project.




Content in
reource_en.txt file:





Menuitem1_name=language


Button1_name=exit


Label1_text=this is 中文版 version!

Content in
resource_zh.txt file:





Menuitem1_name= Language


Button1_name= Exit


label1_text= This is the Chinese version!


Note that the Resource_zh.txt file contains Chinese characters and must be saved in UTF-8 encoding. In Visual Studio. NET integrated development environment, you can click Advanced Save Options on the File menu, select the UTF-8 encoding format, and then save the file.

Then, click Visual Studio on the Programs menu. NET command Prompt menu, enter the DOS mode, enter the following two lines of command to generate the resource files Myapp.resources and myapp.zh-cn.resources that can be accessed in the program. The English resource file myapp.resources as the default fallback resource file. Myapp.zh-cn.resources
Is the Chinese resource file, "MyApp" in the filename is the root name of the resource file, and must be the same as the root name of the fallback resource file, "ZH-CN" specifies the culture of the resource file.

F:\csharp\myapp>resgen resource_en.txt,myapp.resources
F:\csharp\myapp>resgen RESOURCE_ZH.TXT,MYAPP.ZH-CN.
Resources

v. Modify code



1, add the namespaces you want to reference 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 in 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 resource in the corresponding resource file to modify the text display on the Interface menu, label, and button.





private void Readresources () {


This.menuItem1.Text = rm. GetString ("Menuitem1_text", CI);


This.button1.Text = rm. GetString ("Button1_text", CI);


This.label1.Text = rm. GetString ("Label1_text", CI);


this. Refresh ();


}


4, in Form1 Load event processing, according to the contents of the configuration file, invoke the Readresources method, read resources, 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;


 }


Sr. Close ();


this. Readresources ();


}


5, add a method saveconfig to the Form1 to modify the configuration file. In the MENUITEM2 and menuItem3 Click event procedure, call the S a v e C o n F i g method, write the string "Chinese", "中文版" to the configuration file, and invoke 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 ("中文版");


ci = new CultureInfo ("en");


Menuitem2.checked=false;


menuitem3.checked=true;


this. Readresources ();


}


6, in the button button1 Click event procedure, close the form, end the run program.





private void button1_click (...)


{


this. Close ();


}


Six, compile and run the program





If you manually compile the source program, simply click the "Visual Studio.NET command Prompt" menu, enter the DOS mode, enter the following command, you can generate binary executable file MyApp.exe, and then enter MyApp, you can run the program.





F:\csharp\myapp>csc/out:myapp.exe Form1.cs





If you are running a program in the Microsoft Visual Studio. NET Integrated development environment, you will automatically compile and place the compiled binary executable in the bin\debug subdirectory. At this point, it is necessary to note that the Myappconfig.ini, myapp.resources and myapp.zh-cn.resources These three files are also copied to the Bin\debug\ subdirectory, the program can function correctly.




When the
program first runs, it is an English interface (see Figure 2) and clicking on the menu "Chinese" will become the Chinese interface (see Figure 3). Click the button "Exit", exit the program, run the program again, directly display the Chinese language interface.








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.