0

I need to set my app's locale to Bangla. And to do that, I coded in the following way:

    @Override
    protected void attachBaseContext(Context newBase) {
//        Log.d(TAG, "attachBaseContext: BASE CONTEXT CALLED");
        Locale mLocale = new Locale("bn");
        Locale.setDefault(mLocale);

        Configuration config = new Configuration();
        config.setLocale(mLocale);

        final Context newContext = newBase.createConfigurationContext(config);
        super.attachBaseContext(newContext);
    }

I kept on running my app in debug build variant and it worked fine.

However, after switching to release build variant, it didn't work and all the texts of my app was in English only.

I searched through stackoverflow and found similar issue. I tried out this solution and coded in the following way:

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

    private Context updateBaseContextLocale(Context context) {
//        String language = SharedPrefUtils.getSavedLanguage(); // Helper method to get saved language from SharedPreferences
        Locale locale = new Locale("bn");
        Locale.setDefault(locale);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            return updateResourcesLocale(context, locale);
        }

        return updateResourcesLocaleLegacy(context, locale);
    }

    //    @TargetApi(Build.VERSION_CODES.N_MR1)
    private Context updateResourcesLocale(Context context, Locale locale) {
        Configuration configuration = new Configuration(context.getResources().getConfiguration());
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }

    //    @SuppressWarnings("deprecation")
    private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return context;
    }

    @Override
    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
            // update overrideConfiguration with your locale
            setLocale(overrideConfiguration); // you will need to implement this
        }
        super.applyOverrideConfiguration(overrideConfiguration);
    }

    private void setLocale(Configuration overrideConfiguration) {
        Locale locale = new Locale("bn");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            overrideConfiguration.setLocale(locale);
        } else {
            overrideConfiguration.locale = locale;
        }
    }

But it didn't produce the expected result. I created another simple hello-world project to test this code out. And when I ran it for the first time (in debug variant), the text appeared in Bangla. And I didn't even need to override applyOverrideConfiguration(Configuration overrideConfiguration) like I did in the code above. However, after ran my app in release mode, the same problem occurred. And it persisted even after switching to debug mode.

I tried changing androidx.appcompat:appcompat:1.1.0 to appcompat:1.2.0-alpha02 and appcompat:1.3.0-alpha01 but still no luck.

I used android Pie device and android Q emulator to run my app.

Is changing build variant solely responsible for this? Or is there any bug in my code? I would appreciate if someone shares a better approach to solving this problem.

ganjaam
  • 1,030
  • 3
  • 17
  • 29
  • Try to keep AppCompat classes by adding the below line in "proguard-rules.pro" file: -keep class androidx.appcompat.app.** { *; } – VKostenc Oct 13 '20 at 07:00

1 Answers1

1

The easiest way it's use library like - Localization

But if you want to have your own solution - simply check the library source code and I believe that you will find all answers there.

VKostenc
  • 1,140
  • 14
  • 19