Android training-Supports different languages

Source: Internet
Author: User
Tags xml attribute

This article translated from: http://developer.android.com/training/basics/supporting-devices/languages.html#UseString

Extracting the UI string from your application code to an external file is a good practice. The Android system uses a Resource Directory in each android project to make it easy.

If you use the android SDK to create a project, the tool creates a directory named Res/on the top layer of the project. The res/directory contains subdirectories of various resource types. There are also some default files, such as res/values/strings. XML, which save the string value.

Create a language directory and a string File

To add multi-language support, add a values directory containing the hyphen and the end of the ISO country code to the res/directory, for example, values-ES/is a directory containing regional character resources whose language encoding is "es. The Android system loads the corresponding resources according to the language settings at runtime.

Once you decide the language you want to support, you need to create resource subdirectories and string resource files, such:

Myproject/


Res/


Values/


Strings. xml


Values-ES/


Strings. xml


Values-fr/


Strings. xml

Add the string values of each language to the corresponding file.

At runtime, the android system uses the corresponding string resources based on the current language settings of the User device.

For example, the following is a string resource file in different languages.

English (default language encoding),/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>

Note: You can use language delimiters on any type of resource directories, such as providing localized bitmap resources.

Use string Resources

You can use the name defined by the name attribute of the <string> element in your resource code and other XML files to reference your string resources.

In your source code, you can use the R. String. <string_name> syntax to reference string resources. There are many ways to use this syntax to receive string resources, such:

// Get a string resource from your app's
Resources

String Hello =
Getresources (). getstring (R. String. hello_world );

 

// Or supply a string resource to a method that requires a string

Textview = new textview (this );

Textview. settext (R. String. hello_world );

In other XML files, you can use the @ string/<string_name> syntax to reference string resources in any XML Attribute that receives string values. For example:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

 

 

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.