1

I use this snippet to allow the user to set his favorite locale in the appplication:

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

Problem is that I would like to write a setting that would allow the user to get back to the default langage of the phone.

How is it possible?

Because after using the snippet above and imagine user chose French, I cannot get back the phone locale (which might be english for instance)

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • You are not changing the phone's locale, only the application locale at run time. In other words: When the application closes, and runs again, the locale will be the default previous locale (not the one you set). – zed Feb 27 '17 at 13:28
  • Is there a valid reason to downvote? As I said in first sentence, I know this is application locale.. I never said it was phone locale. – Waza_Be Feb 27 '17 at 13:30
  • 1
    I didn't downvote. There are haters all around mate. – zed Feb 27 '17 at 13:30

4 Answers4

1

I just tried this, my phone locale is US, the toast is shown in french but in the log I still see US, maybe if you don't set the new locale as default it works anyway?

Locale locale = new Locale("fr");
//Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

Toast.makeText(this, android.R.string.cancel, Toast.LENGTH_LONG).show();
Log.d("LOCALE", Locale.getDefault().getCountry());

I've seen using Locale.setDefault() in other questions and answers, now I'm wondering, why would you be required to set the default Locale manually? If that was necessary, wouldn't it be done in updateConfiguration() anyway? this answer is also interesting

Community
  • 1
  • 1
lelloman
  • 13,883
  • 5
  • 63
  • 85
0

How about:

  1. Saving current locale to shared preferences
  2. Force to whatever you want
  3. Use the shared preferences value to move back to original locale
jakubbialkowski
  • 1,546
  • 16
  • 24
  • I can imagine that the user would change phone locale during the process.. If I don't get better answer I will accept yours! – Waza_Be Feb 27 '17 at 13:36
  • So in such scenario, assuming that your app is working you can override the `onConfigurationChanged` in `Activity`. In case when app is closed register the broadcast for `BOOT_COMPLETED` and then check the current locale. (after reboot). Or you can use `ACTION_LOCALE_CHANGED` broadcast for that. – jakubbialkowski Feb 27 '17 at 13:52
0

Firstly this bit of code config.locale = locale; is deprecated, you should use config.setLocale(locale);

Have you tried getting the current device locale with Locale.getDefault().getDisplayLanguage();, and set it once the user chooses the default locale to be the selected language of your application with your code snippet?

Ricardo
  • 9,136
  • 3
  • 29
  • 35
0

In https://stackoverflow.com/a/34675427/519334 I solved the issue "go back to device-languge" by remembering the device-language in a static variable before any app-changes to locale have been taken place:

public class Global {
  public static final Locale systemLocale = Locale.getDefault();
}
Community
  • 1
  • 1
k3b
  • 14,517
  • 7
  • 53
  • 85