Android API Guides---localizing with Resources

Source: Internet
Author: User
Tags locale unsupported contact form home screen

Localization with resources
Quick Tour
Use the resource settings to create a localized application.
Android loads the correct resources for the user's language and locale settings.
If the localized resource is not available, Android loads the default resource.
In this document
Overview: Resource switching in Android
Using Resource localization
Localization Tips
Test the localized application software
You can also see
Localization checklist
Provide resources
Layout
Life cycle of activities
Android will run on many devices in many regions. In order to reach the maximum number of users, your application should handle text, audio files, numbers, currency and graphics in a way that suits the locale your application will be using.


This document describes best practices for localizing Android applications. This principle applies whether you are using ADT with Eclipse, Ant-based tools, or any other IDE-developed application.


You should already have a working knowledge of Java, familiar with Android resource loading, user interface?? XML declarations of elements, considerations of development, such as the life cycle of an activity, and the general principles of internationalization and localization.


This is the best way to use the Android resource framework to localize the application, from the core Java features, as far as possible:


You can convert the content of most or all of the application's user interface into a resource file, such as this document, and provide a description of the resource.
The behavior of the user interface, on the other hand, is driven by Java code. For example, if different, depending on the locale you need to format or sort the user input data, then you will use Java programming to process the data. This article does not include how to locate your Java code.
For short guides in your app localized strings, see training sessions that support different languages.


Overview: Resource switching in Android


Resources are text strings, layouts, sounds, graphics and any other static data that you have on Android?? The requirements of the OID application. An application can include multiple groups of resources, each customized for different device configurations. When the user runs the application, Android automatically chooses and loads the best matching device resources.


(This article focuses on the full description of localization and locale-based resource exchange and all types of configurations that you can specify-screen orientation, touch screen type, and so on-see providing alternative resources.) )


When you write your app:


Create a set of default resources, plus alternatives for use in different locales.
Right Arrow
When the user runs the application:


The Android system selects the device-based language environment resources to load.
When you write your application, you can create default and alternative resources for your application to use. To create a resource, you set the project's RES/directory to a file in a subdirectory that is specifically named.


Why default resources are important


Whenever an application runs in a text language environment where you do not provide a specific locale, Android will load the default string from RES/value/Strings.xml. If this default file does not exist, or if it loses its own application that requires a string, then your application will not run and an error will be displayed. The following example shows what happens when the default text file is incomplete.


Cases:


The Java code of an application refers to only two strings, text_a and Text_b. This application includes the defined English text_a and Text_b localized resource files (RES/value-en/strings.xml). This application also includes for text_a a definition of a default resource file (RES/value/strings.xml), but not for Text_b:


This application may compile without problems. If the resource is missing, such as Eclipse's IDE will not be able to highlight any errors.
When the application is started on the device with the locale set to English, the application may run without problems because the RES/value-en/strings.xml contains the required text string.
However, users will see an error message when the Force Close button is rolled out on devices that have the application set to a language other than English. The application will not load.
To prevent this from happening, ensure that a file exists in a RES/value/Strings.xml, and that it defines each required string. This situation applies to all types of resources, not just strings: you need to create a set of resource files that contain all your application calls to resources-layout, graphics, animations, etc. for information about testing, see testing as the default resource.


Using Resource localization


How to create a default resource


Put the default text for the application with the following location and name of the file:


RES/value/Strings.xml (required directory)


The text string in RES/value/strings.xml should use the default language, which is the language that you most expect users of your application to speak.


The default resource set must also include any default drawing and layout, and can include other types of resources, such as animations.
RES/Draw/(required directory to keep at least one graphics file for application on the Google Play icon)
RES/Layout/(required directories hold the XML file that defines the default layout)
RES/Animation/(required if you have any res/anim-< qualifiers > Folders)
Res/xml/(if you have any Res/xml < qualifiers > folders must)
RES/Health/(if you have any need res/raw-< qualifiers > Folders)


Tip: In your code, check each reference to an Android resource. Ensure that a default resource is defined for each item. Also, make sure that the default string file is complete: A localized string file can contain a subset of strings, but the default string file must contain all.


How to create an alternate resource


A large part of a localized application is the provision of alternative text in different languages. In some cases, you will also provide alternative graphics, sounds, layouts, and other specific language environment resources.


Applications can specify multiple RES/< qualifiers >/catalogs, each with different qualifiers. To create an alternate resource for a different locale, you can use qualifiers to specify a combination of language or language regions. (The name of the resource directory must conform to the provision of alternative resources, otherwise the described naming scheme cannot be compiled.) )


Cases:


Suppose the default language for your application is English. Also assume that you want to localize all the French text in the application, while most of the text in your app (except for the title of the app everything) Japan. In this case, you can create three alternative strings.xml files, each of which is stored in a specific region of the resource directory:


RES/value/Strings.xml
Contains all the strings that the application uses, including the text string named title English text.
RES/value in-fr/strings.xml
Contains all strings, including the title method text.
RES/value in-ja/strings.xml
Contains the Japanese text for all strings except the caption.
If your Java code refers to R.string.title, here is what happens at run time:


If the device is set to any language other than French??, Android will load the title in RES/value/strings.xml file.
If the device is set to French, Android will load the title from the RES/value-FR/strings.xml file.
Please note that if the device is set to Japanese, Android will look for the title in RES/value-ja/strings.xml archive. But since no such string is included in the file, Android will fall back to the default value and will load the English title from the RES/value/strings.xml file.


Is it resource-first?


If there are multiple resource files that match the configuration of the device, Android follows a set of rules that determine which file to use. Where the qualifiers can be specified in the resource directory name, the area is almost always preferred.


Cases:


Suppose an application contains the default settings for graphics and two other sets of graphics that are optimized for different device settings:


RES/Draw/
Contains the default graphics.
RES/Draw Small Land pens/
The included graphics are optimized with devices that are expected from stylus input and have a low-density screen QVGA the landscape.
RES/Draw-ja/
The included graphics are optimized with Japan.
If the application is configured to run on a device that uses Japanese, the robot will load from the reservoir/pull--ja/graphics, even if the device happens to be a desired input from the stylus and has a QVGA low-density screen orientation of the landscape.


Exception: The only qualifier for the priority area during the selection process is MCC and MNC (mobile country Code and mobile network code).


Cases:


Suppose you have the following conditions:


Application code calls for R.string.text_a
Two related resource files are available:
Reservoirs/values in-mcc404/strings.xml, which are included in the application's default language text_a, in this case, English.
RES/value Hi/strings.xml, which includes Hindi text_a.
Applications that run on devices that have the following structure:
The SIM card is connected to a mobile network in India (MCC 404).
The language is set to Hindi (HI).
Android will load from RES/values Mcc404/strings.xml (English) text_a, even if the device is configured for Hindi. This is because Android will prefer to match the language of the MCC during the resource selection process.


The selection process is not always as simple as these examples indicate. See how Android looks for a more nuanced description of the process of finding the most matching resources. All qualifiers are listed in priority order in table 2, which provides alternative resources.


Talking about the Java of resources


In the Java code of the application, you refer to using syntax R.resource_type.resource_name or Android. R.resource_type.resource_name Resources. For more information about this, please visit the resources.


Localization checklist


For a complete overview of the process of localizing and distributing Android applications, see localizing manifest files.


Localization Tips


Work in any area of the design application


You can't assume anything about what their users will run the application device. The device may have hardware that you do not expect, or it may be set to what you do not have, or you cannot test the plan locale. Design the application so that it works or does not work properly regardless of what device it is running on.


Important: Make sure that your application includes a complete set of default resources.


Make sure to include RES/draw/and RES/value/folder (no additional decorations for folder names), contain all the pictures and text that your application will need.


If the application is missing or even a default resource, it will not be set to run on a device that is not supported by the language environment. For example, the default file in RES/value/Strings.xml may lack an application that requires a chord: when the application runs in an unsupported locale and tries to load RES/value/Strings.xml, the user sees an error message and a force close button. As the Eclipse IDE will not highlight such an error, you will not see this issue when you test the application set up to support the locale of the device or emulator.


For more information, see Default resources for testing.


Design a flexible layout


If you need to rearrange the layout to fit a language (for example, Germany has a long history), you can create alternative layouts for that language (for example, RES/Layout-de/main.xml). However, doing so can make your application difficult to maintain. It is better to build a single layout with more flexibility.


Another typical scenario is that it requires something different from the layout of the language. For example, you may have a contact form that should include two name fields when the application runs in Japan, but the three name fields, when the application runs in some other languages. You can handle this problem in one of the following two ways:


Create a field in one of the layouts that you can programmatically enable or disable, based on the language, or
There are major layouts including another layout, including changeable fields. The second layout can have different configurations for different languages.
Avoid creating more resource files and text strings than you need


You may not need a substitute for a specific locale for each resource in your application. For example, layouts defined in a file in RES/layout/main.xml may work in any area, in which case there is no need to create any alternative layout files.


Additionally, you may not need to create alternate text for each string. For example, assume that:


The default language for your application is US English. The application uses the definition of each string, using American English spelling, in RES/value/Strings.xml.
For some important phrases, you have to provide English spelling. You want to use these alternative strings when your application runs on a UK device.
To do this, you can create a small file called RES/value-en-rgb/strings.xml, including only when the application runs in the UK for all the rest of the string and should be different strings, the program will drop back to the default value and use the RES/value/ Defined in Strings.xml.


To find a zone manually using the Android context object


You can use context objects that Android can provide for querying the locale:


。 String region = Context.getresources () getconfiguration () Locale.getdisplayname ().
Test the localized application software


Test on the device


Keep in mind that the device you are testing can be significantly different from the devices offered to consumers in other regions. The language environment available on your device may be different from what is available on other devices. Additionally, the screen resolution and density of the device may vary, which may affect the display of your UI strings and drawings.


To change the locale or language on the device, use the settings application.


Testing in an Emulator


See the Android Emulator for more information on using the emulator.


Creating and using a custom language environment


The "Custom" area is a language/region combination the Android system image is not explicitly supported. (For a list of locales supported by Android platforms, see version notes in the SDK tab.) You can test your application to run in a custom locale in the custom area that is created in the emulator. There are two ways to do this:


Applications that use custom locales, which are accessed from the Applications tab. (After you create a custom region, press and hold the setting name to switch to it.) )
Change to a language environment customized from the ADB shell as described below.
When the emulator is set to an area, is not an image available in the Android system, the system itself is displayed in its default language. Your application, however, should be positioned correctly.


Changing the simulator's locale from adb shell


To change the language environment of the emulator using the ADB shell.


Select the BCP-47 language tag that you want to test and determine, such as the locale, Canadian French is Fr-ca.
Start the emulator.
From the command-line shell on the host computer, run the following command:
ADB's shell
Or if you have a connected device, specify the emulator you want by adding the-E option:
ADB-e Shell
At the ADB shell prompt (#), run the following command:
SetProp Persist.sys.locale [BCP-47 language tag]; stop; sleep 5; start
Replace the corresponding Code section in parentheses from the 1th step.
For example, in Canadian French test:


SetProp Persist.sys.locale fr-ca; stop; sleep 5; start


This causes the emulator to restart. (It looks like a full reboot, but that's not the case.) Once again on the home screen, restart your application (for example, click the Run icon in Eclipse) and the application will launch in the new locale.


Default resources for testing


Here's how to test whether an application includes every string resource it needs:


Set the emulator or device to a language that your application does not support. For example, if the application is in RES/value-FR France has chords/but not in any resource string Spanish/value-es/, then set the simulator's locale to Spanish. (You can use custom locales to apply the settings to an unsupported locale for the emulator.) )
Run the application.
If the application displays an error message and a force Close button, it may look for a string that is not available. Make sure that your RES/value/strings.xml file includes the definition of each string used by the application.
If the test succeeds, repeat it for other types of configuration. For example, if the application has a layout file named RES/layout in land/main.xml, but does not contain a file named RES/Layout port/Main.xml, then set the emulator or device as portrait, and see if the application will run.

Android API Guides---localizing with Resources

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.