Android multi-language adaptation

switch language

The most critical line of code is

AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("en"));

Add values ​​folder

Adapt strings.xml in the values ​​files of multiple countries. The contents of the string key in the default values ​​folder should be adapted to all localized configuration folders as much as possible. As shown below, except values-night, which is adapted to dark mode, all others must be adapted.

Screenshot 2024-02-25 20.44.01.png

Follow the system language setting linkage

Although one line of code can switch the language, it will return to the default language after the application process is killed and the application is restarted. In order to save the state, AndroidManifest.xml needs to be configured. Then, we add the following service in the application tag.

<service 
    android:name="androidx.appcompat.app.AppLocalesMetadataHolderService" 
    android:enabled="true" 
    android:exported="true">
    <meta-data 
        android:name="autoStoreLocales" 
        android:value="true"/> 
</service>

Precautions

So here comes the point, how to perfectly implement real-time updates of languages ​​within the application?

Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

Is this possible?Yes, but it is not recommended. There are bugs. Please experiment by yourself.. If I had finished it by then, I wouldn’t have written this article. Now that you have seen this, I might as well recommend my framework, which has support for multiple languages. There is github.com/dora4/dora/… , which can switch languages ​​​​from all over the world. You can use the updateLang method. I still recommend that you use the packaged system api and use the updateSysLang method. Although the mapping of some languages ​​requires some minor modifications, changing underscores to horizontal lines, the system’s solution may be more stable.

val lang = LanguageUtils.getLangTag(this)
if (lang.equals(LanguageUtils.LANG_ZH_CN)) {
    AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("zh"))
} else if (lang.equals(LanguageUtils.LANG_ZH_TW)) {
    AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("zh-TW"))
} else {
    AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(lang))
}

Where is the above code written? As mentioned above, it is not recommended to restart the activity. The recommended method is to use a message framework such as eventbus to send a message notification. The previous page of the language setting page calls the above code to refresh the interface, because finish will not trigger the life of the previous interface. cycle. Or you can use startActivityForResult and onActivityResult. Other interfaces are recreated when opened and get the default language display.

Dora SDK source code for multi-language support

Let’s take a final look at the source code of Dora SDK.

dora.BaseActivity


@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(LanguageUtils.attachBaseContext(newBase));
}

dora.BaseApplication

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LanguageUtils.attachBaseContext(base));
    if (mAppDelegate == null) {
        mAppDelegate = new AppDelegate(base);
    }
    mAppDelegate.attachBaseContext(base);
}

If you use the dora framework, it will automatically update the localization configuration for you while inheriting the Base class. If you use the system’s update language approach, the above code will not work and will not affect the business logic.

Leave a Reply

Your email address will not be published. Required fields are marked *