Android Internationalization operations

Source: Internet
Author: User

1. Brief introduction

We know that using the. properties file in Java to configure resource files is generally used in Chinese message_zh_cn.properties and English Message_en_ Us.properties two configuration files, then implemented through a series of configurations, not detailed here, in Android is also internationalized, and more convenient, because Android itself takes the XML resource file to manage all the string messages, as long as the messages are provided in different languages, The country corresponds to the content can be.

2, the preparation of resource files

Like the properties file in Java, you can also configure such a file on Android, except that Android is writing a string message in the Res/values directory, in order to give these messages a different language, country version, Developers need to add a few different language countries in the values directory, and the different values folders are named:

  values-Language code-R Country code

Example: VALUES-ZH-RCN (Chinese) and Values-en-rus (English)

If you need to make the picture also internationalization, but also to add several different languages for the drawable directory version of the country, the different drawable folder is named: drawable-language code-R Country code

Build a Strings.xml in the Values-zh-rcn folder to represent Chinese resources

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "OK">Are you sure</string>    <stringname= "Cancel">Cancel</string>    <stringname= "MSG">Hello, little robot!</string></Resources>

Build a Strings.xml in the Valuesen-rus folder to represent English resources

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "OK">Ok</string>    <stringname= "Cancel">Cancel</string>    <stringname= "MSG">hello,android!</string></Resources>
3. Use these string resources in the layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" android:text= "@string/msg"         />        <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" android:text= "@string/ok"         />        <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:text= "@string/cancel"/></LinearLayout>

Then load the layout file in the activity via Setcontentview (R.layout.main)

Operating effect:

First shows the English version, the Android simulator default in English, and then we can in Setting/language&input/language this will be the Android language set to ask Chinese, you can see the Chinese version.

Android Internationalization operations

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.