Blog Park Client UAP development essay-Let yourself and your app have international fan

Source: Internet
Author: User

People are not found in the store to see the high on the application are multilingual support, according to the language of the operating system can be automatically adapted, or through the user's choice to display the corresponding language interface, it is really tall on it. But this is not difficult, through a few simple steps, let your application also tall up, support multi-lingual. So in your future resume, you can also write: for the international market to develop too many language applications. Sounds pretty good!

0. Preparatory work

Before you build multilingual support, first look at the default language option in the Manifest file and set it to the default display language you want.

Next, create a folder that holds the language strings. In the case of the universal App, as shown in, create a Strings subfolder under the Shared folder, and then create subfolders based on the language that your app wants to support, and the subfolder name is consistent with the language encoding. For specific language encoding, you can query the Language Codes page:

Http://msdn.microsoft.com/en-us/library/ms533052%28v=vs.85%29.aspx

If it is not the Universal App, then the Strings folder should be placed in the root directory of the project.

After creating the folder, add "Resources File (. resw)" to each folder. Microsoft recommends using the default file name to make calls in the project. If you have special naming requirements, refer to the How to load string resources (XAML) page:

Http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965323.aspx

When this is done, the framework for multi-lingual support is set up, and the rest is to fill in the content.

1. Fill in the string

In en-US example, we open the resources.resw file of en-our, and we find that it is divided into three columns, namely Name,value and Comment. As the name implies, are string references to the names, strings of the contents and strings of comments or comments. Then we begin to fill in the strings according to our own needs. Where Name and Value are more important, must be filled in, Comment is mainly to enhance readability, not required.

If you need multilanguage support for the interface, which is part of the XAML language definition, then the part of Name is defined as a XAML language definition for each unit of the control. For example:

To support multiple languages for the contents of a text box, that is, text, the Name section must be in nameoftextbox.text format. For example, we want to support the "Essence Bar title" in multiple languages, then we first name the Essence bar title Besttitle, and for Besttitle.text this name fill in the content best. Then the job is done.

For a button control, the text part is called a label (why can't it be called text uniformly?). So strange), so give appbutton_zoom this button to fill in a Appbutton_zoom.label entry.

2. Interface Reference string

With this name, how do we invoke it in the interface?

Very simple, through the Uid implementation.

Find the corresponding section in XAML, add the x:uid= "Besttitle" property, so the system will automatically find the corresponding content in multilanguage support, and the text of the content corresponding to the Text property assigned to the TextBlock. In this way, we do not need to hard code the actual content in the XAML file, just need to modify the corresponding resource file can be, is not very convenient.

<ButtonStyle="{StaticResource Win8topappbarbutton}"Tag= "CNBlogs.Pages.BestPostsPage"Click= "Navbutton_click"Padding= "+">                <button.content>                    <TextBlock>                    <RunText=""FontFamily= "Segoe UI Symbol"/>                    <Runx:uid= "Besttitle"Text=""/>                    </TextBlock>                </button.content>            </Button>

Run a look at the effect, if the system language is English by default, then the content of the display has been as we wish to become English. If it is not English, we can do this by switching the system language, or by setting the application language in the program, and the method of setting up the language is described later in this article.

Corresponding, we also want to add the content of Chinese, so as to guarantee the correspondence of the two languages.

Sharp-eyed reader must have found, not only Text, even FontSize can also be defined? For the Width, font and other simple properties, we can according to the characteristics of different languages, the control according to the language classification of the settings, so that the interface can be more consistent with the characteristics of different languages and usage habits.

3. Background code Reference string

Although the foreground design is primarily implemented by XAML, there are some characters that are partially slipped through, and are implemented in the C # code in the background. For the display of these parts, how do you invoke the contents of the resource file? The answer is also very simple.

First, define the string to invoke in the resource file. It is important to note that it is not possible to use a nameoftextbox.text-like format like a string used in defining XAML, but rather to use a naming convention that is similar to variable naming, and no longer uses "." separated by delimiters. Otherwise the system call will fail.

After completing, we open the background C # code, first add this sentence:

New Windows.ApplicationModel.Resources.ResourceLoader ();

This defines a loader that is responsible for finding the corresponding statement from the resource file.

When we need a string, we replace the part of the string with loader. GetString ("String_name"), where string_name corresponds to NAME defined in the resource file. For example, we need to invoke the "Commenttextblocktext_status1" and "Commenttextblocktext_status2" strings, so we'll make the phrase:

Commentbutton.content = Iscommenttemperarorilyshown? Loader. GetString ("commenttextblocktext_status2"): Loader. GetString ("commenttextblocktext_status1");

You can do it.

4. Setting the program language

If you want to ignore the system language in the Windows Store App, it is also possible to force the program language to be set. We just need to add a sentence to the page:

" ZH-CN ";

You can do it. However, it is important to note that after this code is executed, only the pages that have been opened will take effect, and the language of the previously loaded pages will not change. If you want the global to work, the best way is to restart the application.

A better approach is to create a drop-down menu:

This makes it easy for users to choose their own interface language.

Unfortunately, in Windows Phone there is no such a good global setting, the interface language can only follow the system display language. If you want to reset the interface language, you can only use another black technology.

5. Other

It's a pain to switch back and forth between Chinese and English resource files, especially when the number of interface strings is higher. I recommend a plug-in, can be more convenient for the comparison between the language and translation.

Select Tools–extensions and Updates in Visual Studio, search for Resxmanager this plugin, after the installation reboot, you can open it in the TOOLS menu, as shown in the interface:

Such a control interface is more convenient than the original single language interface, if there is a vacancy in a language entry, he also marked red reminder, is a multi-lingual and localization support tool.

Summary

Localization and multi-language support technology is not difficult, the difficulty is often in the multi-lingual characteristics of the balance, the accuracy of translation and maintenance. This article is only the first step of the Long March, the back of the road also to developers a string to a string to pave ...

Share the code and change the world!

Windows Phone Store App Link:

http://www.windowsphone.com/zh-cn/store/app/Blog Park-UAP/500F08F0-5BE8-4723-AFF9-A397BEEE52FC

Windows Store App Link:

http://apps.microsoft.com/windows/zh-cn/app/c76b99a0-9abd-4a4e-86f0-b29bfcc51059

GitHub Open Source Link:

Https://github.com/MS-UAP/cnblogs-UAP

MSDN Sample Code:

Https://code.msdn.microsoft.com/CNBlogs-Client-Universal-477943ab

Blog Park Client UAP development essay-Let yourself and your app have international fan

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.