Android multilingual settings

Source: Internet
Author: User

Android multilingual settings

The multilingual switchover function was used in recent projects. Note that in the xml file and code, all the places where the text string is used ensure that the string text used is from the string in the values folder. in xml.

Therefore, two to three files are required for multiple languages. 1. The string. xml file in the values folder stores the default language type that your app uses when there is no language setting. 2 values-en folder (this folder is created by yourself, of course, it can also be another name string. xml. Here it stores the English text corresponding to each text. 3values-tw (Traditional Chinese)

Note that the text variables in the string. xml file under the three folders must be consistent in number and Corresponding names.

The following is the code for switching. I selected an important explanation.

Public class ChangeLanquageActivity extends BaseActivity implementsOnClickListener {private RelativeLayout cnBtn, twBtn, enBtn; private ImageView attributes, attributes, attributes; private Button leftBtn; private TextView topTtitle; SharedPreferences Sp; // Save the SP for language switching so that the current language type @ Overrideprotected int getContentLayout () {return R can be sent in subsequent network requests. layout. activity_language ;}@ Overrideprotected void initGui () {LeftBtn = (Button) findViewById (R. id. leftBtn); topTtitle = (TextView) findViewById (R. id. topTitle); cnBtn = (RelativeLayout) findViewById (R. id. cnBtn); twBtn = (RelativeLayout) findViewById (R. id. twBtn); enBtn = (RelativeLayout) findViewById (R. id. enBtn); cnBtn_choose = (ImageView) findViewById (R. id. cnBtn_choose); twBtn_choose = (ImageView) findViewById (R. id. twBtn_choose); enBtn_choose = (ImageView) findVie WById (R. id. enBtn_choose); Sp = getSharedPreferences ("userInfo", this. MODE_WORLD_WRITEABLE);} @ Overrideprotected void initAction () {cnBtn. setOnClickListener (this); twBtn. setOnClickListener (this); enBtn. setOnClickListener (this); leftBtn. setOnClickListener (this) ;}@ Overrideprotected void initData () {// specifies the text of the button for initializing the language to be switched. language is the global variable used to save the current Language environment. 0, 1, and 2 represent three languages. setText (R. string. language_s Et); if (StringUtils. equals (MBConfig. language, "0") {// traditional Chinese cnBtn_choose.setVisibility (View. INVISIBLE); twBtn_choose.setVisibility (View. VISIBLE); enBtn_choose.setVisibility (View. INVISIBLE);} else if (StringUtils. equals (MBConfig. language, "1") {// english cnBtn_choose.setVisibility (View. INVISIBLE); twBtn_choose.setVisibility (View. INVISIBLE); enBtn_choose.setVisibility (View. VISIBLE);} else if (StringUtils. eq Uals (MBConfig. language, "2") {// Simplified Chinese cnBtn_choose.setVisibility (View. VISIBLE); twBtn_choose.setVisibility (View. INVISIBLE); enBtn_choose.setVisibility (View. INVISIBLE) ;}@ Overridepublic void onClick (View v) {// click the event and switch the language configuration. The switch configuration is saved with sp for use next time. You do not need to consider the hidden logic of buttons in the middle. Resources resources = getResources (); Editor edit = Sp. edit (); switch (v. getId () {case R. id. leftBtn: hideProgressDialog (); finish (); break; case R. id. cnBtn: cnBtn_choose.setVisibility (View. VISIBLE); twBtn_choose.setVisibility (View. INVISIBLE); enBtn_choose.setVisibility (View. INVISIBLE); changeAppLanguage (resources, "CN"); MBConfig. language = "2"; edit. putString ("LANGUAGE", "2"); edit. commit (); hideProgressDial Og (); finish (); break; case R. id. twBtn: cnBtn_choose.setVisibility (View. INVISIBLE); twBtn_choose.setVisibility (View. VISIBLE); enBtn_choose.setVisibility (View. INVISIBLE); changeAppLanguage (resources, "TW"); MBConfig. language = "0"; edit. putString ("LANGUAGE", "0"); edit. commit (); hideProgressDialog (); finish (); break; case R. id. enBtn: cnBtn_choose.setVisibility (View. INVISIBLE); twBtn_choose.setVisibility (View. INVI SIBLE); enBtn_choose.setVisibility (View. VISIBLE); changeAppLanguage (resources, "EN"); MBConfig. language = "1"; edit. putString ("LANGUAGE", "1"); edit. commit (); hideProgressDialog (); finish (); break; default: break;} public void changeAppLanguage (Resources resources, String lanrecognition) {// Method for language switching Configuration config = resources. getConfiguration (); // read the system configuration information DisplayMetrics dm = resources. getDisplayMetrics (); if (la NAtr. equals ("CN") {config. locale = Locale. CHINA;} else if (lanasd. equals ("TW") {config. locale = Locale. TAIWAN;} else if (lanasd. equals ("EN") {config. locale = Locale. ENGLISH;} else {config. locale = Locale. getDefault ();} // set resources in three languages. updateConfiguration (config, dm); // refresh the language settings. This step must be minimal .}

 

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.