loshkin
loshkin

Reputation: 1630

Runtime app localization doesn't work with app bundles

I have different string values for different languages. At runtime, the user can choose another language. minSdkVersion 21

I am updating the Locale with these methods

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language); // persists new language in shared prefs
    return updateResources(c, language);
}

private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language, language + "_" + language.toUpperCase());
    Locale.setDefault(locale);
    Resources res = context.getResources();
    Configuration config = res.getConfiguration();
    config.setLocale(locale);
    context = context.createConfigurationContext(config);
    return context;
}

And have a BaseActivity where I've overridden attachBaseContext, every activity extends it.

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
}

And also have overridden in BaseApplication

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base)); //updates resources
    MultiDex.install(getBaseContext());
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleManager.setLocale(this); //updates resources
}

After the user chooses the new language I am recreating the activity. While all this works fine on debug when I download the release build from the store the app uses only the default language. And changing language does nothing.

Upvotes: 2

Views: 516

Answers (1)

loshkin
loshkin

Reputation: 1630

First, the problem is not with the code but with the change of how Android deals with resources with the new app bundles. Previously with the APKs all resources were downloaded. Now it downloads only the resources that your phone is eligible for. If you have in preferred languages only English it won't download other languages unless... you say otherwise

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
    }
    density {
        // This property is set to true by default.
        enableSplit = true
    }
    abi {
        // This property is set to true by default.
        enableSplit = true
    }
}

Upvotes: 1

Related Questions