Android apps _android with multiple language switching capabilities

Source: Internet
Author: User

Recently in doing a multilingual switching function, similar to the micro-letter language switch, search the data are basically the following:

1. Effect of implementation


Similar to the micro-letter, in the settings interface to open the switch language interface, the choice of language after the restart Homeactivity, language switch completed, the next time the APP, is also the user settings language.

2. Implementation steps
1). adding MultiLanguage Files
Adding string.xml files in different languages under different value folders (such as value, Value-en, VALUES-ZH-RTW folders), our project adds three languages in English, Simplified Chinese, and traditional Chinese, as shown in the following illustration:

English translation, traditional if not specifically translated, you can find a simple conversion site, directly Simplified Chinese into traditional Chinese, I use this site: Online Chinese Simplified to traditional.

2). Update the Locale property in Configuration
Referring to the Android developer Web Description, Configuration contains all the configuration information for the device, which affects the resources the application obtains. For example, a string resource, which is based on the Configuration locale attribute, determines which language the string resource is to be taken from, by default under the Value folder.

The main code is as follows:

resources = GetContext (). Getresources ();
Displaymetrics DM = Resources.getdisplaymetrics ();
Configuration config = resources.getconfiguration ();
 Application User Selection language
 config.locale = locale.english;
 

We used the preset values in Locale locale.english, Locale.traditional_chinese, and Locale.simplified_chinese, and if you need to set a language that has no preset values, you can create a new Locale object, specifically Google it.

Note: Follow system setup is Locale.getdefault ()

3). Restart Homeactivity
Our App has a startup page welcomeactivity, similar to the micro-letter that villain launch page, if you restart from the Welcome page, not a good experience, should be the same as the micro-letter language settings, directly back to homeactivity, rather than from welcomeactivity Open again. The implementation is actually very simple, the code is as follows:

Intent Intent = new Intent (this, homeactivity.class);
Intent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task);

Normally this code should be fine, but if your App has an activity and the current Settings page activity is not within a task stack (for example, an activity you start with flag_activity_new_task from a notification page) , the language settings are not applied. So you can kill the current APP process and make sure it's all restarted:

 Intent Intent = new Intent (this, homeactivity.class);
 Intent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task);
 StartActivity (intent);
 Kill Process
 Android.os.Process.killProcess (Android.os.Process.myPid ());
 

Kill the process by reason of two lines of code can be any line, but check the relevant information, or two add it, if there is a detailed understanding welcome to communicate. This code actually refers to the Customactivityoncrash Open source project, interested in the open Source Library to capture the crash information, restart the application section of the code.

Personally feel that this method of restarting homeactivity is too rough and bad experience, but it seems that the micro-letter is so restarted homeactivity. General language switching settings will be hidden deep, such as micro-letter, if a user is not familiar with the operation of the mobile phone or the application is not familiar, accidentally switched to do not know what language and then give me a jump back to the homepage, and then want to set back to Chinese on the trouble. So, I think it is best to finish the language immediately refresh and then stay on the current page better. Specifically implemented as follows:
1, or to add a multilingual file, the same as the above steps 1;
2. Implement the language switch code in baseactivity:

@Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Eventbus.getdefault (). Register (this);
    Changeapplanguage ();
  } 
public void Changeapplanguage () {
     String sta = Store.getlanuageischinese ()? "en": "en";//This is the Sharedpreferences tool class, used to save the settings, the code is very simple, its own realization bar
     //local Language settings
     Locale mylocale = new Locale (STA);
     Resources res = getresources ();
     Displaymetrics DM = Res.getdisplaymetrics ();
     Configuration conf = res.getconfiguration ();
     Conf.locale = Mylocale;
     Res.updateconfiguration (conf, DM);
   
 public void OnEvent (String str) {
     switch (str) {case
       constant.event_refresh_language:
         Changeapplanguage ();
         Recreate ();//refresh interface break
         ;
    }
  
 @Override
   protected void OnDestroy () {
    Super.ondestroy ();
     Eventbus.getdefault (). Unregister (this);
  

3, set the interface of the implementation of the nature is to update the settings in Sharedpreferences, and then send Eventbus on the line

 if () {//Chinese
       Store.setlanuageischinese (true);
     } else if () {//English
       Store.setlanuageischinese (false)
     ;
     Eventbus.getdefault (). Post (Constant.event_refresh_language);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.