Android language Internationalization

Source: Internet
Author: User

Objective

We often use the Android phone system in the "settings" item will provide users with a variety of system language choices, such as: Chinese, English ... You may have found that these languages are displayed in the order of "left to right", but some languages are "left-to-right", such as Arabic, Hebrew and other languages, how do we handle layout files when we are in this locale? The next time we're going to discuss the issue of dealing with "right to left" languages such as Arabic.

Layout

First of all, we explain the layout problem, in the Android system in order to support the display of different languages, you can define specific (Arabic, Hebrew ...). ) Language layout file, specifically:


The map Layout-ar corresponds to the Arabic layout file, so we just need to modify the layout file in that directory to achieve our realistic effect in the Arabic language environment. Before Android4.2, we need to copy a layout file for each "Right to left" language, Layout-fa Layout-iw and so on, the disadvantage is that there will be a lot of redundant layout files.

Android4.2 version, Google for the Arab and other Bidi languages (note: Bidi language is "right to left" written language) view and text layout file made significant changes, similar to 2D graphics acceleration mechanism, can be controlled from activity/application , Window code control, view resource control a total of 3 levels of control. All layout files for Bidi languages can be placed in the Layout-ldrtl directory:


Layout-ldrtl is a layout directory that Google specifically reserved for the Bidi language, so directories such as Layout-fa LAYOUT-IW can be deleted directly after the Android4.2 version. You only need to define the Bidi language layout in the Layout-ldrtl directory.

Activity/application Control

In the manifest file we can set the Android:supportsrtl property, for details, please refer to the following code:

    <application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @style/apptheme "        android:supportsrtl=" true ">

View Control

To set the Android:layoutdirection property in the layout file of the Layout-ldrtl directory, please refer to the following code:

<linearlayoutxmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: Layoutdirection= "RTL" >    <textviewandroid:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:text=" @string/hello_world "android:layoutdirection=" RTL "/></linearlayout>
Note: Layout-ldrtl priority is introduced, LAYOUT-LDRTL priority is lower than the language level (Layout-ar), so if there is a Layout-ar directory, the first to find the Layout-ar directory layout file, It is therefore recommended to delete Bidi language directories such as Layout-ar. Layout-ldrtl priority is higher than the resolution level, the priority is summarized as follows:

Layout-ar (Language level) > Layout-ldrtl > layout-800*540 (resolution)

Code control

After introducing the above two kinds of static control modes, we take a look at the dynamic Control Bidi language display mode, please refer to the following code:

        if ("Ar". Equals (Locale.getdefault (). GetLanguage ())) {            GetWindow (). Getdecorview (). Setlayoutdirection ( VIEW.LAYOUT_DIRECTION_RTL);        }
The above is the Bidi language involved in the layout of the modification and configuration, combined with the above methods can solve our internationalization encountered in the Bidi language problems.

Hashcode when you read here, there may be questions about hashcode and language. In fact, hashcode problem is the final decision I write this blog purpose. Speaking of Hashcaode, let's start by recalling the role of Hashcode in Java, simply saying that Hashcode is the unique identity of the identity object (remark: non-rigorous statement). Recently in the development of Android system encountered such a problem, when the phone after taking pictures of the normal display (icon display camera icon) should be shown in 1-1:

Fig. 1-1 Fig. 1-2

However, in the Azeri and Turkish locales are displayed as folder icons, as shown in 1-2, after the investigation of the final discovery is the following code in different locales hashcode different causes, the specific code is as follows:

    public static int Getbucketid (String path) {        return path.tolowercase (). Hashcode ();    
The following changes will be displayed as normal, the code is as follows:

    public static int Getbucketid (String path) {        return path.tolowercase (locale.english). Hashcode ();//Use English character conversion    }
Android's explanatory information is as follows:

Converts this string to lower case, using the rules of the locale.
Most case mappings is unaffected by the language of a Locale. Exceptions include dotted and dotless I in Azeri and Turkish locales, and dotted and dotless I and J in Lithuanian locales . On the other hand, it isn ' t necessary to provide a Greek locale to get correct case mapping of Greek Characters:any Local e'll do.
It probably means that the character encoding used in the Azerbaijani and Turkish locales is different, and we have to set the character encoding to a uniform local encoding so that the hashcode remains consistent

For specific codes, please refer to: http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt

Summary: Android language internationalization summed up so much, I hope to help you, if you have new ideas or better ways to achieve language internationalization, please comment in the commit, we discuss together.

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.