Android Learning Route (17) support for different devices--support for different languages

Source: Internet
Author: User
Tags locale

It is a good practice to extract strings from the UI in the application code and store them in additional files. Android in each project through a resource directory makes this matter very simple.

If you created a project using the Android SDK tool, the tool creates a directory in the top-level directory of your project res/ . In this directory there are many subdirectories for storing multiple types of resources. There are also default files, for example res/values/strings.xml , to hold strings.

Creating regional directories and string files

In order to support more languages, create additional directories under the res/directory values , with the file names using values plus hyphenation symbols plus international standardized language codes. For example, the values-es/ directory contains a simple resource file for the area with the language code "ES". The Android system loads the appropriate resource files based on the locale settings set when the device is running. For more information, see providing alternative Resources.

Once you have decided to support this language, create the appropriate resource directory and string file for it. For example:

myproject/    res/       values/           strings.xml       values-es/           strings.xml       values-fr/           strings.xml

Adds a string value to the file for each zone.

At run time, theAndroid system loads the appropriate resource files based on the locale settings set when the device is running.

For example, the following are different resource files for different languages.

中文版 (default region), /values/strings.xml :

<?XML version="1.0"encoding="Utf-8"?><resources>    <string name="title">My Application</string>    <string name="Hello_world">Hello world!</string></resources>

Spanish, /values-es/strings.xml :

<?XML version="1.0"encoding="Utf-8"?><resources>    <string name="title">Mi Aplicación</string>    <string name="Hello_world">Hola mundo!</string></resources>

French, /values-fr/strings.xml :

<?XML version="1.0"encoding="Utf-8"?><resources>    <string name="title">Mon Application</string>    <string name="Hello_world">Bonjour Le monde!</string></resources>

tip: You can use zone limits (or any configuration restrictions) for any type of resource, such as if you want to provide different bitmap drawable for different regions. For more information, see Localization.

Using string resources

You can <string> name reference the resource in the code or other XML file using the string name specified in the attribute of the element.

In your source code, you can use R.string.<string_name> the syntax to reference a string resource.

For example:

//Get A string resource from your app ' sResourcesStringHello= getResources().getString(R.string.Hello_world);//or supply a string resource to a method, that requires a stringTextViewTextView= New TextView( This);TextView.SetText(R.string.Hello_world);

In other XML files, you can use @string/<string_name> syntax to reference string resources.

For example:

<textview    Android:layout_width="Wrap_content"    Android:layout_height="Wrap_content"    Android:text="@string/hello_world" />

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.