Android for multi-language switching

Source: Internet
Author: User

In Android development, you often encounter situations in which you need to switch between different languages. Language differences mean that dynamic switching of characters in different languages and dynamic switching of different layouts are required.

This is a very simple feature for Android:

First, in the res create other languages corresponding to the value, layout and other folders, such as the addition of English and Vietnamese version, named such as Value-en-us, Value-vi, layout-en-us, Layout-vi.

The naming rules can be found online, value-en-us, en in English, US for the United States. The country is not written. The Vietnamese version can be fully written as LAYOUT-VI-VN.

So, there are several sets of the same name, with the same ID, but the content properties are different from the multi-version layout.

Then, write the code.

Import Java.util.Locale;

Locale locale;

String language = Locale.getdefault (). GetLanguage ();

if (language.equals ("en")) {

...

}

Can be used in the OnClick event of the switching language:

Case *:

locale = Locale.english;

Or: locale = new locale ("VI", "VN"); Viet Nam

Restart Change language:

private void Changelanguage (locale locale) {

try {

Iactivitymanager am = Activitymanagernative.getdefault ();

Configuration config = am.getconfiguration ();

Config.locale = locale;

Config.usersetlocale = true;

Am.updateconfiguration (config);

} catch (RemoteException e) {log.e (TAG, e.tostring ()); }  }

This completes the Code section.

Finally, change the compilation section configuration file to add language support:

/build/target/product directory, language_full.mk|language_small.mk see your compilation options using that file, modify Product_locales

In the Full.mk file, there is this fragment: $ (call inherit-product, build/target/product/languages_small.mk), and the latter path is where we want to set it.

The above is excerpted from other blogs,

In fact, in my compilation environment, add VI_VN to Locales_full.mk.

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.