Android internationalization (forced conversion language within the Program)

Source: Internet
Author: User

First, let's take a look:

 

Project Structure:

 

Android 3.0 and earlier versions:

Import java. util. locale;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. context;
Import Android. content. dialoginterface;
Import Android. content. intent;
Import Android. content. sharedpreferences;
Import Android. content. sharedpreferences. Editor;
Import Android. content. res. configuration;
Import Android. content. res. Resources;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. adapterview. onitemselectedlistener;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. spinner;

Public class hellomyl10nactivity extends activity {
Private sharedpreferences preferences;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// If (savedinstancestate = NULL ){
// Savedinstancestate = getintent (). getbundleextra ("instance ");
// If (savedinstancestate! = NULL ){
// Onrestoreinstancestate (savedinstancestate );
//}
//}
Preferences = getsharedpreferences ("Lang", context. mode_private );
String langstring = preferences. getstring ("Lang", "en ");
Setlanguage (langstring );
Setcontentview (R. layout. Main );

Button B;
(B = (button) findviewbyid (R. Id. flag_button). setbackgrounddrawable (this. getresources (). getdrawable (R. drawable. Flag ));

// Language selection
Final string Lang [] = new string [] {"en", "FR", "de", "Ja "};
String items [] = new string [] {"English", "French", "German", "Japanese "};
Spinner spinner = (spinner) findviewbyid (R. Id. chooselang );
Spinner. setadapter (New arrayadapter <string> (this, Android. R. layout. simple_spinner_item, items ));
If (preferences. getint ("Lang", 0 )! = 0 ){
Spinner. setselection (savedinstancestate. getint ("position"), true );
}
Spinner. setonitemselectedlistener (New onitemselectedlistener (){

@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
If (! (Preferences. getstring ("Lang", "en"). Equals (Lang [position]) {
Setlanguage (Lang [position]);
Editor editor = preferences. Edit ();
Editor. putstring ("Lang", Lang [position]). Commit ();

Intent intent = new intent (hellomyl10nactivity. This, hellomyl10nactivity. This. getclass ());
Startactivity (intent );
Finish ();
}

}

@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub
}
});
}


Private void setlanguage (string langstring ){

Resources resources = getresources ();
Configuration Config = resources. getconfiguration ();
Locale locale = new locale (langstring );
Locale. setdefault (locale );
Config. locale = locale; // Simplified Chinese
Resources. updateconfiguration (config, null );
}

}

This method does not work when it comes to Android 4.0. The text effect is still internationalized, but the image is not internationalized.

 

I recommend a blog http://blog.csdn.net/a_colt/article/details/6193857 to check whether the problem exists.

"But at the same time I found a problem.

If a button event is triggered

The directory button generated in the same activity does not work, but does work for the dialog box that has not yet been generated before triggering. Do you need to refresh the directory menu to generate after triggering? "

 

Finally, upload a tool class for international conversion. The following configurations are required for use:/files/lee0oo0/languagesettingutil.rar

Categoragesettingutil. INIT (getactivity (); categoragesetting = categoragesettingutil. Get (); // get the final string cursyslanguage = locale. getdefault (). getlanguage ();

Deleagesetting. checksyschanged (cursyslanguage );

 

// Set the language for replacement

Receivagesetting. savelanguage ("ZH ");

// Update

Languagesetting. refreshlanguage ();
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.