The multi-language switch refers to the multi-language switch within an application, and does not involve the function of modifying system language Settings through an application. Like in wechat

I -> Settings -> General -> Multilingual For example, if the App supports both Simplified and traditional Settings, the default interface is “Simplified Chinese”.

If the user selects Simplified Chinese, the simplified Chinese interface is displayed. If the user chooses “Traditional Chinese”, the traditional interface will be displayed. If the user selects Follow System, if the system language is Set to Simplified Chinese, the simplified Chinese screen is displayed. If the system language is set to Traditional Chinese, the traditional Chinese screen is displayed. If the system language is set to English(US), the simplified Chinese screen is displayed.

android_push

In addition, during the running of the App, if the user switches the language in the system Settings, the application “language Settings” will also be prompted to change, so there is a flow chart:

android_push

The difference between this diagram and the previous one is that if the user does not select “Follow the system”, the Settings from the system are ignored.

The issues that need to be addressed are as follows:

How do I obtain the in-app language Settings? How do I obtain the System language Settings? How do I change the app language Settings

  1. The problem of how to obtain the in-app language Settings is easier to handle by saving the user’s language Settings in SharedPreference.

class UserLocale{ public static final int FOLLOW_SYSTEM = 0; Public static final int SIMPLIFIED_CHINESE = 1; Public static final int TRADITIONAL_CHINESE = 2; // Traditional Chinese

// Get user Settings
public int getUserLocale(a){
    / /...
}Copy the code

}

  1. How do I get system language Settings

    First: locale.getDefault () This is a method from Java that can be changed with locale.setdefault (Locale). This method, however, sometimes has puzzling problems, such as returning the system language “zn_CN” and “en_US”, which is not stable.

Context.getresources ().getConfiguration().locale; context.getResources().getConfiguration(). For Android N, it should

context.getResources().getConfiguration().getLocales().get(0); This returns the Locale setting for the Resource of the current APP. This can also be changed in code. Update the language Settings of the application by updating the Configuration. Again, the problem is that we definitely need to change the Configuration, and once we do, this operation will no longer get the system language Settings. If you must use this, a workaround is to save the Configuration at the beginning and update it synchronously every time the system language changes.

Resources.getsystem ().getConfiguration().locale; This method is very similar to the above method, except that this method returns the Locale of the system’s global Resource, which can be used as the system Locale.

  1. How do I change the language Settings of my application

    As mentioned above, after the user selects the language, the Configuration corresponding to the APP should be modified:
     Locale targetLocale = getTargetLocale();
     Resources resources = context.getResources();
     Configuration config = resources.getConfiguration();
     DisplayMetrics dm = resources.getDisplayMetrics();
     config.locale = targetLocale;
     resources.updateConfiguration(config, dm);Copy the code

As for the getTargetLocale() implementation:

int getTargetLocale(a){
    int userType = UserLocale.getUserLocale();
    if(userType == LanguageType.FOLLOW_SYSTEM){
        int sysType = getSysLocale();
        if(sysType == LanguageType.TRADITIONAL_CHINESE){
            return Locale.TRADITIONAL_CHINESE;
        }else{
            returnLocale.SIMPLIFIED_CHINESE; }}else if(userType == LanguageType.TRADITIONAL_CHINESE){
        return Locale.TRADITIONAL_CHINESE;
    }else{
        returnLocale.SIMPLIFIED_CHINESE; }}Copy the code

For the implementation of getSysLocale(), see “2. How to Obtain System Language Settings”.

Language switching can be triggered from two sources:

For In-App Language Switching, after the user selects the language, you need to manually restart all screens. For convenience, you can directly restart Root Activity and Clear Top.

For “System Switch Language Settings”, by default, if our APP is running in the background, the system will actively rebuild all activities. We can listen on the Root Activity

Intent.ACTION_LOCALE_CHANGED indicates that the global Configuration of the APP is changed before restarting the Activity.

If the corresponding Activity is set

Android :configChanges=”locale

Activity. OnConfigurationChanged (Configuration newConfig) in response to changes in the Configuration. If not, see The Android :configChanges Locale Configuration Does not Work After the Language Change. For more details, see the Android- Language Setting Process.

RadioButton or CheckBox did not change the language after the rebuild. Not all items in the layout update properly when switching locales is the cause of this problem

This is mainly due to setFreezesText(true), but it seems to have been fixed since 5.0 because the source code of CompoundButton in the Framework has been changed:

Android 4.4.1 _r3:

@Override
public Parcelable onSaveInstanceState(a) {
    setFreezesText(true);
    Parcelable superState = super.onSaveInstanceState();

    SavedState ss = new SavedState(superState);

    ss.checked = isChecked();
    return ss;
}Copy the code

The Android 6.0:

@Override
public Parcelable onSaveInstanceState(a) {
    Parcelable superState = super.onSaveInstanceState();

    SavedState ss = new SavedState(superState);

    ss.checked = isChecked();
    return ss;
}Copy the code