5

I have implemented this code to change the language of the App and it works correctly in the emulator with API 23, 24, 25, 26, 27 and 28 and in the real device also when installed in "Active Build Variant" mode in "debug" state

Info: Android N change language programmatically

But when I upload the app to Google Play Store in "Active Build Variant" mode in "release" state the language change does not work on the real device

That could be happening ? I don't know what to do with this problem anymore. I appreciate if you can help me. Thanks

My method static ContextWrapper, class Config:

    public static ContextWrapper changeLang(Context context, String lang_code){
    Locale sysLocale;

    Resources rs = context.getResources();
    Configuration config = rs.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        sysLocale = config.getLocales().get(0);
    } else {
        sysLocale = config.locale;
    }

    if ( !lang_code.equals("") ) {
        Locale locale = new Locale(lang_code);
        Locale.setDefault(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale);
        } else {
            config.locale = locale;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            context = context.createConfigurationContext(config);
        } else {
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }
    }

    return new ContextWrapper(context);
}

My Activity code

    @Override
protected void attachBaseContext(Context newBase) {
    pref = PreferenceManager.getDefaultSharedPreferences( newBase );
    String lang_code = pref.getString("LANG_APP","");

    Context context = Config.changeLang(newBase, lang_code);
    super.attachBaseContext(context);
}

That could be happening ? I don't know what to do with this problem anymore. I appreciate if you can help me. Thanks

Luis
  • 91
  • 6

2 Answers2

2

This solution worked for me

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
    }
}
}

The problem may be Android BUNDLE, if you build BUNDLE (not apk) it removing language .xml in apk file. If you write above code, language.xml not removed when you create .aab bundle file.

MarsPeople
  • 1,772
  • 18
  • 30
0
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
    super.applyOverrideConfiguration(baseContext.resources.configuration)
}

I had the same problem, adding this to base activity fixed it for me

Khangin
  • 11
  • 2