Android Application resource-localization (2)

Source: Internet
Author: User

Use localized Resources

How to create default Resources

Put the default text of the application in a file with the following position and name:

res/values/strings.xml

Text strings in RES/values/strings. xml should use the default language, which is the language that most application users are expected to speak.

The default resource that must be set also includes any other descriptive and layout resources and animation resources.

Res/drawable/(this directory must contain at least one graphic file for the Android app startup icon)

Res/layout/(this directory stores XML files that define the default layout)

Res/anim/(if needed, you can have any Res/anim-<qualifiers> folder)

Res/XML/(if needed, you can have any Res/XML-<qualifiers> folder)

Res/raw/(if needed, you can have any Res/raw-<qualifiers> folder)

Tip: Check each referenced Android resource in the code. Ensure that each resource has a default resource definition, and ensure the integrity of the default string file. The localized string file can be a subset of all strings, but the default string file must contain all strings.

How to Create available resources

Most of the work of localized applications is to provide optional texts for different languages. In some cases, you also need to provide optional graphics, sound, layout, and resources for other specific environments.

Applications can use different delimiters to specify many Res/<qualifiers>/directories. You can create optional resources for different language environments by using a combination qualifier of a specific language or language plus a region. (The name of the Resource Directory must be the naming scheme described in the "provide available resources" document, otherwise it will not be compiled .)

For example:

Assume that the default language of the application is English. It is also assumed that the text in the application should be localized to French, and the application should be applied to a Japanese environment (except for the title of the application ). In this case, you need to create three alternative strings. XML files, each of which is saved in the specified resource directory:

1. Res/values/strings. xml: contains all the English text used by the application, including the name text of the title.

2. Res/Values-fr/strings. xml: contains all the French text of the string, including the title.

3. Res/Values-Ja/strings. xml: Contains Japanese text containing all strings except the title.

If the R. String. Title resource is applied to the Java code, the following situation occurs during running:

1. If the device is set to any language other than French, Android will load the title from the res/values/strings. xml file.

2. if the device is set to a French language environment, Android will load the title from the res/Values-fr/strings. xml file.

Note that if the device is in a Japanese environment, Android first starts from Res/vaues-Ja/strings. search for titles in the XML file, but because the file does not contain a title string, Android returns the default title, that is, from Res/values/strings. load the title resource in the XML file.

Which resource does Android choose first?

If multiple resource files match the device configuration, Android uses the following rules to determine which resource file to use. Some of them specify a qualifier in the Resource Directory name, and the language environment is always preferred.

For example:

Assuming that the application contains a default Graph Set and two groups of other images, each group is optimized for different devices:

1. Res/drawable/: contains the default image.

2. Res/drawable-small-land-stylus/: contains images for handwriting input and ovga low density horizontal screen devices.

3. Res/drawable-Ja/: contains images suitable for Japanese environments.

If the application is running on a device with a Japanese environment configured, even if the device expects to use a pen and the screen is a low-density screen of qvga, then android will load the image from the res/drawable-Ja/directory.

Exception: only the MCC (mobile country code) and MNC (mobile network code) delimiters have higher priority than the language environment.

For example:

Assume the following scenarios:

1. The application code calls R. String. text_a.

2. Two resources are available:

Res/values-mcc404/strings. XML, which contains the default language of text_a in an English environment in an application.

Res/Values-Hi/strings. XML, which contains text_a strings in Hindi.

3. The application runs on the following configured devices:

The SIM card is connected to India's mobile network (MCC 404 ).

The language environment is set to Hindi (HI ).

Even if the device is configured in Hindi, the android system loads text_a from Res/values-mcc404/strings. XML (English resources. This is because the MCC has a higher priority than the language in the resource selection process.

The selection process is not always as straightforward as this example. For more information, see "how to find and match the best resources" in "Android Application resources-access resources ". All the delimiters are described in table 2 in "Android Application resources-provide resources.

Reference resources in Java code

In the Java code of the application, the syntax for referencing resources is: R. resource_type.resource_name or android. R. resource_type.resource_name (Android system resource ).

Localization policy

Design applications that can work in any language environment

Nothing can be assumed on the device where the application is running. The device may have hardware you don't expect, or set up a language environment that you don't plan or have not tested. Therefore, it is necessary to design a program so that it can run normally or be friendly to failures on any device.

Important: ensure that the application contains all default resource sets.

Ensure that the Res/drawable/and Res/values/folders containing all the images and text required by the application are included (the folder name does not contain any other modifier ).

If the application encounters a default resource error, it will not run in the unsupported device environment. For example, Res/values/strings. XML default files may lack the strings required by an application. The application runs in an unsupported language environment and tries to load Res/values/strings. XML file, you will see an error message and a force close button. IDE such as eclipse will not find such errors, and will not find this problem when testing on a device or simulator that supports a language environment.

Flexible Layout Design

To adapt to a certain language (such as long words in German), you can create an optional layout for this language (such as res/Layout-de/main. XML ). However, this will make the application more difficult to maintain. A better way is to create a more flexible single layout.

Another typical scenario is that different languages have different requirements for layout. For example, when an application is running in a Japanese environment, you need a communication record format that contains two name fields. When running in other languages, you need three name fields. There are two ways to handle this situation:

1. Create a layout with three fields, and enable or disable one of the name fields programmatically based on the language environment.

2. Create a primary layout that contains a sub-layout with another variable field. The second layout can be configured for different languages.

Avoid creating redundant resource files and text strings

In an application, it is impossible to create optional resources for a specific language environment for each type of resource. For example, the layout defined in the Res/layout/Main. xml file can work in any language environment, so there is no need to create an optional layout resource file for any language environment.

Also, you do not need to create optional text for each string. For example, assume that:

1. The default language of the application is American English. Each string used by the application uses American English spelling, Which is saved in the Res/values/strings. xml file.

2. For some important phrases, you want to provide English spelling. This allows applications to use these optional resources when running on UK devices.

To do this, you need to create a small file named Res/Values-en-RGB/strings. XML, which only contains strings different from those running in the United States. For all other strings, the application uses the default string defined in the Res/values/strings. xml file.

Use the android context object to manually find the language environment of the device

You can use the context object provided by Android to find the language environment of the device:

String locale
= Context. getresources (). getconfiguration (). locale. getdisplayname ();

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.