0

I am suffering from a problem

I am setting Apps language using :

@TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);
        Timber.d("LangCheck updateResources  " + locale.getLanguage());
        Timber.d("LangCheck updateResources  " + locale.getDisplayLanguage());


        return context.createConfigurationContext(configuration);
    }

    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        configuration.setLayoutDirection(locale);

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        Timber.d("LangCheck updateResources  " + locale.getLanguage());
        Timber.d("LangCheck updateResources  " + locale.getDisplayLanguage());
        return context;
    }

After changing , I am able to use in selected language

But after restarting App , I am getting only English. i.e , the resources.getString(..) gave English only even though the above functions run at every startup in base activity

I tried to find the language of the App using : Locale.getDefault().displayLanguage . It's returning 'português' . It's correct

But , if I use resources.configuration.locale.displayLanguage , I am getting 'inglês' !!!

Why this conflict ? That is ,

Locale.getDefault().displayLanguage            =>  português 
resources.configuration.locale.displayLanguage =>. inglês 

I think since context language is still in English , I am getting getString () -> English Strigs only

Why this conflict ?

pls help me ?

RagAnt
  • 1,064
  • 2
  • 17
  • 35
  • i don't think this question has anything to do with android studio as an IDE, so i've removed the tag, feel free to add it back if you think that's incorrect – a_local_nobody Jan 07 '21 at 08:50

1 Answers1

0

The difference you described comes from the two different ways you used to set the Locale: Locale.setDefault(locale) and configuration.setLocale(locale).

The first one changes the locale and stores it in a static field, which means it is updated for the whole process, and will not be changed until the app process is killed, or if Locale.setDefault is called again.

configuration.setLocale(locale) updates the locale only for the context you are returning. A context you used is probably an Activity context, which means the configuration locale is only changed for that one activity. When you "restart" the app, a new Activity is created, but the app process may not be killed (possible, but doesn't always happen), and that's why you see the difference between the two locale fields.

The Configuration's locale is what Android uses when using Context.getString(), so that's why after restarting you see the strings in English.

For much more details on how to change the locale at runtime, and common pitfalls with doing so, here's a decent article about it: https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758

Maurice Lam
  • 1,577
  • 9
  • 14
  • The above functions (context.createConfigurationContext) run at every startup in base activity and attached to the base Activity context using super.attachBaseContext(...) – RagAnt Jan 07 '21 at 09:23
  • https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx this fixed my issue – RagAnt Jan 07 '21 at 10:04
  • Thanks for that article . It's very informative – RagAnt Jan 07 '21 at 10:04