Android realizes the multi language switching function of APP applications

Recently, we are doing a multi language switching function, which is similar to the language switching of wechat. The following data are basically the following:

1. Effect achieved

Similar to wechat, open the language switching interface in the setting interface, select the language and restart homeactivity. The language switching is completed. The next time you reopen the app, it is also the language set by the user.

2. Implement step 1) Add multilingual files. Add strings in different languages under different value folders (such as value, value en, values zh RTW folders) XML file, our project adds three languages: English, simplified Chinese and traditional Chinese, as shown in the figure below:

English needs to be translated. If there is no special translation, you can find a simple and complex conversion website to directly convert simplified Chinese into traditional Chinese. This website I use: Online Chinese from simplified to traditional.

2). Update the locale attribute in the configuration. Refer to the description on the Android Developer's official website. The configuration contains all the configuration information of the device, which will affect the resources obtained by the application. For example, a string resource is a string resource that determines which language to use according to the locale attribute of the configuration. By default, it is in the value folder.

The main codes are as follows:

We used the default value locale in locale ENGLISH、Locale. TradITIONAL_ Machine and locale SIMPLIFIED_ Machine, if the language you need to set does not have a default value, you can create a new locale object by yourself.

Note: the following system setting is locale getDefault()

3). Restart homeactivity our app has a startup page, welcomeactivity, which is similar to the villain startup page of wechat. If you restart from the welcome page, it is not a good experience. You should go back to homeactivity directly like the language setting of wechat, rather than reopen from welcomeactivity. The implementation is also very simple. The code is as follows:

Normally, this code should be OK, but if there is an activity in your app and the activity of the current setting page is not in the same task stack (for example, an activity you start from a notification page with flag_activity_new_task), the language setting will not be applied. Therefore, you can directly kill the process of the current app to ensure that the "whole" is restarted:

Kill any one of the two lines of code of the process according to the reason, but check the relevant materials, or add both. If you have a detailed understanding, please communicate. This code actually refers to 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.

Personally, I think this method of restarting homeactivity is too rough and has a bad experience, but it seems that wechat restarted homeactivity in the same way. General language switching settings are hidden. For example, wechat. If a user is unfamiliar with mobile phone operation or this application, he accidentally switches and doesn't know what language, and then jumps back to the home page. If he wants to set it back to Chinese, it's troublesome. Therefore, I think it's better to refresh immediately after cutting the language, and then stay on the current page. The specific implementation is as follows: 1. You still need to add multilingual files, just like step 1 above; 2. Implement language switching code in baseactivity:

3. The implementation in the setting interface is naturally to update the settings in shared preferences, and then send eventbus

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>