Windows 8 Study Notes (22)-Global Resources

Source: Internet
Author: User

In terms of project requirements, we often need to implement multi-language applications. The terminology is resource globalization. In the winrt library, the corresponding methods for global resources are also provided in the namespace windows. Globalization.
Next we will implement a simple globalization step by step:
1. Prepare our resources. Before that, there is a "BCP-47" name that needs to be known. The name of our resource Language Pack needs to be named according to BCP-47 rules. For example, the Chinese package name is ZH-CN and the English package name is en-us.
Create a new strings folder in the project, create a new ZH-CN and en-US folders under the folder, and create a resource. resw resource file under the two folders respectively.
2. resource files and control displays are associated with the control UID to create a blank page resourcepage. XAML

<Stackpanel orientation = "horizontal" horizontalalignment = "center" verticalignment = "center">
<ComboBox margin = "10, 10, 0, 13" grid. column = "0" selectedvaluepath = "tag" name = "langcombobox" horizontalalignment = "Left" verticalalignment = "TOP" width = "106" Height = "34" selectionchanged = "langcombobox_selectionchanged">
<Comboboxitem tag = "En-us"> English </comboboxitem>
<Comboboxitem tag = "ZH-CN"> Chinese </comboboxitem>
</ComboBox>
<Textblock X: uid = "tbwelcome" X: Name = "tbwelcome" horizontalalignment = "Left" margin = "20, 0, 0, 0 "textwrapping =" Wrap "fontsize =" 30 "foreground =" red "verticalignment =" TOP "Height =" 49 "width =" 287 "/>
</Stackpanel>

3. Add Resource content:

Chinese:

Add similar English letters

 

4. In the project file package. appxmanifest, you can set the default language. To test, we added a ComboBox to switch between the two languages.

Private void langcombobox_selectionchanged (Object sender, selectionchangedeventargs E)
{
String Language = convert. tostring (langcombobox. selectedvalue );
If (Windows. Globalization. applicationlanguages. primary1_ageoverride! = LANGUAGE)
{
Windows. Globalization. applicationlanguages. primary1_ageoverride = language;
ResourceManager. Current. defaultcontext. Reset ();

Resourceloader loader = new resourceloader ();
Tbwelcome. Text = loader. getstring ("tbwelcome/text ");
}
}

The preceding figure shows a simple control display globalization. Of course, the use of globalization is not only language display, but also the display width of controls in different languages, for example, the Finnish language is longer than the English characters, so we can make the width of the control a whole, for example, the above control can be added to the resource file tbwelcome. the value corresponding to width.
We noticed that when switching between different languages, we mainly divided into two steps: Set the preferred language of the application to the selected language, and then use resourceloader to load the corresponding resource package, use this resource package to load resources of different controls.

For resourceloader, you can also manually specify the corresponding resource, resourceloader resource = new resourceloader (. resw file name.

When it comes to resource loading, there is another method that has to be mentioned. This method enables the application to automatically match the most suitable resources. For example, there may be many resource files, you can use the following method to filter applications:

Resourcecontext context = new resourcecontext ();
If (context. qualifiervalues. containskey ("language "))
{
Context. qualifiervalues ["language"] = language;
}
Else
{
Context. qualifiervalues. Add ("language", language );
}

Resourcemap resourcestringmap = ResourceManager. Current. mainresourcemap. getsubtree ("Resources ");
Resourcecandidate = resourcestringmap. getvalue ("tbwelcome/text", context );
If (resourcecandidate! = NULL)
{
Tbwelcome. Text = resourcecandidate. valueasstring;
}

Resourcecontext indicates the upper and lower associated resources of the current application. You can use the corresponding language in the resource to obtain the required resource values.

 

There is also a usage to switch resources. You must first specify the primarylanguageoverride value as the current language. The usage is as follows:

String Language = convert. tostring (langcombobox. selectedvalue );
If (Windows. Globalization. applicationlanguages. primary1_ageoverride! = LANGUAGE)
{
Windows. Globalization. applicationlanguages. primary1_ageoverride = language;
ResourceManager. Current. defaultcontext. Reset ();
Tbwelcome. Text = ResourceManager. Current. mainresourcemap. getvalue ("Resources/tbwelcome/text"). valueasstring;
}

Of course, there are also globalization of date, time, and currency as the display forms of each country are also different. These are relatively simple, you only need to refer to some templates in the API for formatting. The Microsoft examples are provided ~

 

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.