Android app for multi-language switching

Source: Internet
Author: User
Tags eventbus

Recently in doing a multi-language switching function, similar to the language switch, the search for the following information is basically:

1. Effect of the implementation

and similar, in the setting interface to open the interface of switching language, select the language after the restart Homeactivity, the language switch is complete, the next time you reopen the APP, is also the user set language.

2. Implement step 1. Add Multilingual Files

Add files in different languages under different value folders (for example, value, Value-en, Values-zh-rtw folder), string.xml our project is added in English, Simplified Chinese, Traditional Chinese three languages, as shown in:

One of the English needs to translate, traditional if there is no special translation, you can find a simple and complex conversion website, directly to the simplified Chinese to Traditional Chinese, I use this site: Online Chinese Simplified to traditional.

2. Update the Locale property in the Configuration

As described on the Android Developer's Web site, the configuration contains all of the information about the device, which affects the resources that the app obtains. For example, a string resource, which is based on the locale property of the Configuration, determines which language the string resource is in, and defaults to the value folder.

The main code is as follows:

1 Resources resources = GetContext (). Getresources (); 2 displaymetrics dm = Resources.getdisplaymetrics (); 3 Configuration Config = Resources.getconfiguration (); 4//Application User Select language 5 config.locale = locale.english;6 Resources.updateconfiguration ( Config, DM);

We used the Locale default values in, Locale.ENGLISH Locale.TRADITIONAL_CHINESE and Locale.SIMPLIFIED_CHINESE , if you need to set the language has no default value, you can create a new Locale object yourself, specifically Google bar.

Note: Following the system settings isLocale.getDefault()

3. Restart Homeactivity

Our App has a launch page welcomeactivity, similar to that villain launch page, if the restart from the Welcome page is not a good experience, should be the same as the language settings, directly back to the homeactivity, rather than from the welcomeactivity re-open. Implementation is actually very simple, the code is as follows:

1 Intent Intent = new Intent (this, homeactivity.class); 2 Intent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task); 3 getactivity (). StartActivity (Intent);

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 (such as when you use a startup activity from a notification page FLAG_ACTIVITY_NEW_TASK  ), the language setting is not applied. So you can directly kill the current APP process, and ensure that the "entire" Restart:

1 Intent Intent = new Intent (this, homeactivity.class); 2 Intent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task); 3 startactivity (Intent); 4//Kill process 5 android.os.Process.killProcess ( Android.os.Process.myPid ()); 6 system.exit (0);

According to the reason to kill the process of two lines of code any line can be, but access to relevant information, or two plus it, if there is a detailed understanding of the welcome communication. This code is in fact referenced from the Customactivityoncrash Open source project, interested in the next open Source Library to capture the crash information, restart the application part of the code.

Personally feel that this restart Homeactivity method is too rough and experience is not good, but it seems to be so restarted homeactivity. General language switching settings will be hidden deep, for example, if a user is not familiar with the mobile phone operation or said to the application is not familiar, accidentally switched to do not know what language and then give me to jump back to the homepage, and then want to set back the Chinese on the trouble. So, I think it's best to cut out the language immediately and then still stay on the current page better. The specific implementation is as follows:

1, or to add a multilingual file, as above in step 1;

2. Implement the language switch code in baseactivity:

@Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Eventbus.getdefault (). Register (this);        Changeapplanguage ();    }
1 public void Changeapplanguage () {2         String sta = Store.getlanuageischinese ()? "zh": "en";//This is the Sharedpreferences tool class, used to save the settings, the code is very simple, its own implementation of it 3         //local Language Settings 4         locale Mylocale = new locale (STA); 5         Resources res = getresources (); 6         displaymetrics dm = Res.getdisplaymetrics (); 7         Configuration conf = res.getconfiguration (); 8         Conf.locale = Mylocale; 9         res.updateconfiguration (conf, DM);     
1 public void onEvent (String str) {2         -switch (str) {3 case             Constant.event_refresh_language:4                 Changeapplanguage (); 5                 recreate ();//Refresh Interface 6                 break;7         }8     }
1 @Override2     protected void OnDestroy () {3         Super.ondestroy (); 4         Eventbus.getdefault (). Unregister (this); 5     }

3, the implementation of the settings interface is naturally the update sharedpreferences settings, and then send Eventbus on the line

1        if () {//Chinese 2             Store.setlanuageischinese (TRUE); 3         } else if () {//English 4             Store.setlanuageischinese ( FALSE); 5         }6         eventbus.getdefault (). Post (Constant.event_refresh_language);

The basic idea is this, if there is any better way, please give me a message, let me worship a bit.

Classification:Android NotesLabel:Android, language switch

Android app for multi-language switching

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.