Internationalization and localization of Android

Source: Internet
Author: User
Tags i18n

Internationalization (international) is referred to as i18n, because there are 18 characters between I and N, localization (localization), or l10n. Generally, the language of a region is in the format of region _, such as zh_cn and zh_tw.

Country abbreviation http://www.loc.gov/standards/iso639-2/php/code_list.php, country and country abbreviated http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html

 

Android provides excellent support for i18n and l10n. Android does not have a dedicated API to provide internationalization, but uses the naming of different resources to achieve internationalization. At the same time, this naming method can also be used to differentiate hardware, for example, use different images for different new screens.

In eclipse projects, the res directory has several default resources, such as drawable, layout, menu, and values.

Other

Res/anim/used for animation placement
Res/XML/is used to place XML definitions such as style theme.
Res/raw/used to store data

 

When referencing these resources, we use the R. resource_type.resource_name method in Java code, such as settitle (R. String. main_title );

Another method is to directly reference data in XML, such

<Textview
Android: Id = "@ + ID/hello_view"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
References a string named hello. When the program is running, the resource class will load a string named hello in strings. xml. However, when the resource class loads strings. XML, it selects the XML file to be loaded Based on the settings of the current mobile phone. These mobile phone settings include:

MCC and MNC, language and region, screen dimensions, wider/taller screens, screen orientation, screen pixel density, touchscreen type, SDK version, that is, through the current language area of the mobile phone, the screen size, screen pixels and the version number of the current Android SDK. By placing strings. XML in a folder named after these options, the resource class can correctly load the required strings.

We use different local and region for internationalization. First, values indicates the default string. That is, when the resource cannot find the matched resource, resources in the values folder are used by default, other resources such as drawable are the same.

 

When naming the names of these folders, different options are separated with-, and the order must be the same as the order of table 2 in the andorid document, see http://developer.android.com/guide/topics/resources/resources-i18n.html#AlternateResources

Folder names must all be lowercase characters; otherwise, errors may occur in some file systems with sensitive sizes. If you use uppercase letters, the eclipse ADT will automatically report errors. The lower-case letter R indicates the meaning of region. It is named in simplified Chinese, traditional Chinese, and default options. In strings. XML, except for the character string itself, the name of the character string is defined in XML, and the IDs are the same. So when you reference this resource in code or XML, you only need to reference the name or ID. When the program starts, the resource class returns to the corresponding directory to find the correct string (resource)

 

 

By setting locale & text in settings, we can let the resource class select the content in the corresponding folder,

 

 

 

When selecting English, the result is as follows, that is, strings. xml under values is selected.

 

When selecting Chinese (China), strings. xml under the values-ZH-RCN directory is selected.

 

 

 

When you select Chinese (Taiwan), strings. xml under the values-ZH-RTW directory is selected.

 

 

 

You can create directories for other mobile phone options, such as screen pixels.

 

 

Related Article

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.