Android app multi-language switch function implementation, androidapp

Source: Internet
Author: User
Tags eventbus

Android app multi-language switch function implementation, androidapp

Recently, I was working on a multi-language switch function, similar to the language switch. I found that the information is basically the following:

 

1. Implementation results

Similarly, open the language switching interface on the setting interface, select the language, and restart HomeActivity. The language switching is complete. The next time you open the App, you can also set the language.

2. Implementation Step 1. Add a multi-language file

Addstring.xmlFile. Three languages are added to our project: English, simplified Chinese, and traditional Chinese, as shown in:

English translation is required. If there is no specific translation for traditional Chinese, you can find a simplified and traditional conversion website to directly convert Simplified Chinese to traditional Chinese. I use this website: Online Simplified Chinese to traditional Chinese.

2. Update the locale attribute in Configuration

According to the description on the Android developer website, Configuration contains all the Configuration information of the device, which affects the resources obtained by the application. For example, the string resource is determined based on the locale attribute of Configuration, which is the string resource in the value folder by default.

The main code is as follows:

 

1 Resources resources = getContext (). getResources (); 2 DisplayMetrics dm = resources. getDisplayMetrics (); 3 Configuration config = resources. getConfiguration (); 4 // language selected by the application 5 config. locale = Locale. ENGLISH; 6 resources. updateConfiguration (config, dm );

 

We usedLocaleDefault Value inLocale.ENGLISH,Locale.TRADITIONAL_CHINESEAndLocale.SIMPLIFIED_CHINESEIf the language you want to set does not have a preset value, you can create a newLocaleObject, Google it.

Note: The following system settings are as follows:Locale.getDefault()

3. Restart HomeActivity

Our App has a launch page WelcomeActivity, similar to the startup page of the villain. If restarting from the welcome page is not a good experience, we should go back to HomeActivity directly like the language settings, instead of restarting WelcomeActivity. The 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 is okay, but if your App has an activity that is not in the same task stack as the current set page activity (for example, if you useFLAG_ACTIVITY_NEW_TASK The language setting is not applied. Therefore, you can directly kill the process of the current App and ensure that the process is "whole" and restarted:

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 );

It is reasonable to kill any line of the two lines of code of the process, but check the relevant information and add it to both. If you have a detailed understanding, please contact us. For more information about this code, see the CustomActivityOnCrash open-source project. If you are interested, you can study this open-source library to capture crash information and restart the application code.

 

 

I personally think this method of restarting HomeActivity is too crude and has a poor experience, but it seems that it has restarted HomeActivity. Generally, the language switching settings are hidden. For example, if a user is unfamiliar with the mobile phone operation or the application, he or she accidentally switches over and does not know the language, and then jumps back to the homepage, if you want to set it back to Chinese, it will be troublesome. Therefore, I think it is better to immediately refresh the language and stay on the current page. The specific implementation is as follows:

 

1. Add a multi-language file, which is the same as step 1;

 

2. Implement the language switching 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. It is used to save the settings. The code is very simple. 3 // set Locale myLocale = new Locale (sta) in the local language ); 5 Resources res = getResources (); 6 DisplayMetrics dm = res. getDisplayMetrics (); 7 Configuration conf = res. getConfiguration (); 8 conf. locale = myLocale; 9 res. updateConfiguration (conf, dm); 10}
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 setting interface naturally updates the settings in sharedPreferences and sends EventBus.

1 if () {// Chinese 2 Store. setLanuageIsChinese (true); 3} else if () {// english 4 Store. setLanuageIsChinese (false); 5} 6 EventBus. getDefault (). post (Constant. EVENT_REFRESH_LANGUAGE );

This is the basic idea. If there is any better method, please leave a message and let me worship it.

 

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.