Development of New Fashion Windows 8 (6): Resources & Localization

Source: Internet
Author: User

Old week's blog column: http://blog.csdn.net/tcjiaan

For more information, see the original author and source.

 

In actual development, we may need to make such an interface, which can be divided into two situations:

1. The application has multi-language versions (such as simplified Chinese, traditional Chinese, and English). The text displayed on the user interface will automatically apply resources according to the system conditions, for example, if my system is in simplified Chinese, use the content in the resource file in simplified Chinese.

2. You can select a language such as simplified Chinese or traditional Chinese. Dynamically loads strings in the resource file based on the language selected by the user.

 

Just like the example I did for this article, after running it, "simplified Chinese" is selected by default, that is, the text on the page is displayed as simplified Chinese.

 

Then, select "Traditional Chinese". At this time, the text on the interface also changes accordingly.

 

Well, the general effect is like this.

Next, let's take a look at this instance and use it to understand its mysteries.

1. Start vs 2012 and create an empty page application project (47 words are omitted here ).

2. Create a new folder named "strings" in the project, and then create two folders named "ZH-Hans" and "ZH-HANT" under the new folder. Note, do not enter an incorrect name. The former is simplified Chinese and the latter is traditional Chinese.

3. Create a resource file in each of the two folders created above. The file name can be set to resources. resw by default. This facilitates reference in the code. (Right-click the folder and select "new item" from the menu to find "resource file ").

If your operations are correct, the directory structure of your project resources is as follows.

Remember, whether you are using French, Japanese, Chinese, or bird, the name of the resource file must be the same, but stored in different folders, the folder is named in a language mark (such as ZH-CN), which is so simple. Do not make a mistake.

 

4. First open the resource file under simplified Chinese en-Hans, enter three items, then open the resource file under en-HANT, and enter three items. Remember, the key names must be the same, but the values are different. When the language is used, the resources of the ox language are input into the ox language, and the resources used as the bird language are input into the bird language, just like the translation.

As shown in.

 

5. Open mainpage. XAML and paste the layout code without explanation.

<Page X: class = "app2.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app2" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <page. resources> <style X: key = "tbtitle" targettype = "textblock"> <setter property = "fontsize" value = "20"/> <setter property = "margin" value = "4,20, 0, 3 "/> </style> <style X: key = "strdisplaytb" targettype = "textblock"> <setter property = "fontsize" value = "28"/> <setter property = "textwrapping" value = "Wrap"/> </style> </page. resources> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "25"> <ComboBox X: name = "cbselectlang" width = "235" horizontalalignment = "Left"> <comboboxitem> Simplified Chinese </comboboxitem> <comboboxitem> traditional Chinese </comboboxitem> </ComboBox> <textblock text = "first text: "Style =" {staticresource tbtitle} "/> <textblock X: Name =" txtbtext1 "style =" {staticresource strdisplaytb} "/> <textblock text =" second text: "Style =" {staticresource tbtitle} "/> <textblock X: Name =" txtbtext2 "style =" {staticresource strdisplaytb} "/> <textblock text =" third item text: "Style =" {staticresource tbtitle} "/> <textblock X: name = "txtbtext3" style = "{staticresource strdisplaytb}"/> </stackpanel> </GRID> </Page>

I believe you can understand the above XAML. If you cannot understand it, it is necessary to review WPF.

 

6. Open the Code view (mainpage. XAML. CS) and bind an event handler to ComboBox in the constructor.

        public MainPage()        {            this.InitializeComponent();            this.cbSelectLang.SelectionChanged += cbSelectLang_SelectionChanged;        }

7. The code for cbselectlang_selectionchanged is as follows:

Void cbselectlang_selectionchanged (Object sender, selectionchangedeventargs e) {ComboBox cb = sender as ComboBox; If (CB! = NULL) {string applanguage = string. empty; Switch (CB. selectedindex) {Case 0: applanguage = "ZH-Hans"; break; Case 1: applanguage = "ZH-HANT"; break; default: applanguage = "ZH-Hans "; break;} // change the language resourcecontext rscontext = ResourceManager. current. defaultcontext; rscontext. ages = new list <string> (New String [] {applanguage}); // rscontext. qualifiervalues ["language"] = applanguage; // load the resource resourceloader loader = new resourceloader (); this.txt btext1.text = loader. getstring ("Item1"); this.txt btext2.text = loader. getstring ("item2"); this.txt btext3.text = loader. getstring ("item3 ");}}

 

You can use the preceding code to set the language of the application.

Resourcecontext rscontext = ResourceManager. Current. defaultcontext;
Rscontext. Ages = new list <string> (New String [] {applanguage });
At the same time, I commented on a line below.

// Rscontext. qualifiervalues ["language"] = applanguage;
That is to say, both methods can modify the preferred language.

1. Set the ages attribute of resourcecontext;

Second, qualifiervalues of resourcecontext is a dictionary set. We can modify the value of the Middle-Key to language to achieve the same effect.

 

How are the Uris and structures of application resources and the identifiers of referenced resources distributed? In addition, the next loop is decomposed. 88

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.