Resource file localization

zhaozj2021-02-16  74

The resource file is one of the effective ways to solve the localization.

Create a blank project.

First create a form. Wave two button.

The first Button is used to switch the language. The second Button is used to display the corresponding language resources.

Check the Form's properties. In the miscellaneous, there is a language. We first chose Chinese (Simplified). Then edit the text of the form is "this is an example." Button1's Text is "Switching Language", button2's text is "getting information". save. Then select English (US), the editing form of Text is "this is a example", button1.text is "Switch Language", button2.text is "Get Info". save.

This information is stored below the folder of the en-CH and EN-US below Bin / Debug.

Then we switch back to the code page, please take a look at the initializecomponent () method. This method is not the same as a form of ordinary (no language properties). Look at the following code, it tries to read the form related information from the resource file.

System.Resources.ResourceManager Resources = New System.Resources.ResourceManager (TypeOf (Form1));

.................................

this.button2.Text = resources.GetString ( "button2.Text"); this.button2.TextAlign = ((System.Drawing.ContentAlignment) (resources.GetObject ( "button2.TextAlign"))); this.button2.Visible = ((BOOL) ("Button2.Visible")))))));

.................................

We write button1 Click events as follows:

if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == "en-US") {System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture ( "zh-CN") ;?} else {System.threading.thread.currentthread.currentuicultuicultuicultuicultunicuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultuicultunicultuicultuicultuicultuicultuicultuicultuicultunicuicultuiculture = system.globalization.cultureInfo.createspecificculture ("en-us");?}

// This code can be copied from the InitializationComponent method. Information used to reset the form.

System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Form1)); this.button1.Location = ((System.Drawing.Point) (resources.GetObject ( "button1.Location"))); this. Button1.size = ("button.drawing.size); this.button1.text = resources.getstring (" Button1.Text "); this.button1.textalign = (Resources.getObject ("Button1.TextAlign"))))))))); this.Button2.Location = ((System.drawing.Point) ("Button2.Location")); this .Button2.size = ((System.drawing.Size) ("Button2.Size")); this.button2.tabindex = ((int))))))) This.Button2.text = resources.getstring ("Button2.Text"); this.button2.textalign = ((System.drawing.ContentAlignment) ("Button2.TextAlign"))); this.Text = Resources.getstring ("$ this.text"); then add two resource files to the project, namely myResource.resx and myResource.en-us.resx.

Double-click MyResource.en-US.RESX to add a content. Add One in the name bar, and you add one in Value.

Give myResource.resx, Name is also One, and Value is "one".

Then write the click2 Click event.

as follows:

ResourceManager RM = New ResourceManager ("Resourcemgr.myResource", this.gettype (); assemblybox.show (rm.getstring ("one");

OK. Run this form, click the button to view the effect.

转载请注明原文地址:https://www.9cbs.com/read-14990.html

New Post(0)