Android App Bundle with in-app locale change

2019-01-27 19:03发布

I've a problem with AAB when I need to change the app locale from within the app itself(i.e. have the language change setting inside the app), the issue is that the AAB gives me only my device languages resources, for example:

my device has English and French languages installed in it, so AAb gives me only the resources for English and French,

but from within the app itself there is a choice to switch the language between English, French, and Indonesian,

in that case, when changing the language to English or French everything is working perfectly, but when changing it to Indonesian, the app simply enters a crash loop as it keep looking for Indonesian language but it can't find.

The problem here is that even if I restarted the app, it enters the crash loop again as the app is still looking for the missing language resources, and here the only solution is to clear cash or reinstall which are the solutions that the normal user won't go through.


Just to mention it, this is how I change the locale through the app:

    // get resources
    Resources res = context.getResources();
    // create the corresponding locale
    Locale locale = new Locale(language); // for example "en"
    // Change locale settings in the app.
    android.content.res.Configuration conf = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLocale(locale);
        conf.setLayoutDirection(locale);
    } else {
        conf.locale = locale;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        context.getApplicationContext().createConfigurationContext(conf);
    }
    res.updateConfiguration(conf, null);

P.S. The app is working perfectly when build it as APK.

2条回答
乱世女痞
2楼-- · 2019-01-27 19:31

For the time being, you can disable the splitting by language by adding the following configuration in your build.gradle

android {
    bundle {
        language {
            // Specifies that the app bundle should not support
            // configuration APKs for language resources. These
            // resources are instead packaged with each base and
            // dynamic feature APK.
            enableSplit = false
        }
    }
}

Better solutions coming soon.

查看更多
疯言疯语
3楼-- · 2019-01-27 19:43

This is not possible with app bundles: Google Play only downloads resources when the device's selected languages change.

You'll have to use APKs if you want to have an in app language picker.

查看更多
登录 后发表回答