Darksymphony
Darksymphony

Reputation: 2683

Android set app language according the device language

I have an app, where the app language is slovak if the user's device language is set to Slovak, otherwise it will be EN. So the language is not changing in app, but is from instaliing SK or EN.

 String languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
 String lng = "sk".equals(languageToLoad) ? "sk" : "en";

 Locale locale = new Locale(lng);Locale.setDefault(locale);
 Configuration config = new Configuration();config.locale = locale;
 getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

I noticed, that locale in Configuration has been deprecated and also updateConfiguration(Configuration, DisplayMetrics) in Resources has been deprecated from android N (I think).

However for some reason it still works also for higher Android versions, but Android studio says it is deprecated.

So I tried to separate the code:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        String languageToLoad  = Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
        String lng = "sk".equals(languageToLoad) ? "sk" : "en";
        Locale locale = new Locale(lng);Locale.setDefault(locale);
        Configuration config = new Configuration();config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    } else {
  String languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
  String lng = "sk".equals(languageToLoad) ? "sk" : "en";

  Locale locale = new Locale(lng);Locale.setDefault(locale);
  Configuration config = new Configuration();config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

but the first condition (for >=N) is not ok, I am not sure how to replace the current code. It still says the updateConfiguration is deprecated.

Even if I try getBaseContext().createConfigurationContext(config); it doesn't change the language

Upvotes: 0

Views: 3912

Answers (2)

Darksymphony
Darksymphony

Reputation: 2683

Ok, so I solved it this way:

created a ContextWrapper in separate java file:

public class ContextWrapper extends android.content.ContextWrapper {

public ContextWrapper(Context base) {
    super(base);
}

public static ContextWrapper wrap(Context context, Locale newLocale) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);

        context = context.createConfigurationContext(configuration);

    }  else {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }

    return new ContextWrapper(context);
}

}

and then in my MainActivity I used it like this:

     @Override
       protected void attachBaseContext(Context newBase) {

        String languageToLoad;
        String lng;
        Locale locale;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             languageToLoad = Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
        }
else {  languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
        }

         lng = "sk".equals(languageToLoad) ? "en" : "sk";
         locale = new Locale(lng);Locale.setDefault(locale);

        Context context = ContextWrapper.wrap(newBase, locale);
        super.attachBaseContext(context);
    }

Seems working fine now

Upvotes: 0

Perraco
Perraco

Reputation: 17360

When API >= N, replace this:

getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

by this:

context.createConfigurationContext(config);

Yet, you must create a custom context wrapper, and ensure the activity is restarted. See the next accepted answer:

Android N change language programmatically

Upvotes: 1

Related Questions