0

I have a project which has the core and also a library

I change my locale in run time in my core project.

The problem is , in the library project there is a

Locale.getDefault(); 

which only return the locale of the device but not the locale of the app. That means, when I change the locale to French in my app , if my device use English , the locale get by library project is still English. How to fix it? Thanks

user3538235
  • 1,991
  • 6
  • 27
  • 55

1 Answers1

2

You can change the default locale.

You can try with the below code:

Locale locale = new Locale("fr"); //if you want to change to French
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

Or make a locale setter method:

 public void setLocale(String newLocale) {
    Locale locale = new Locale(newLocale);
    Locale.setDefault(locale);

    Configuration config = new Configuration();
    config.locale = locale;

    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics() );
 }
ztan
  • 6,861
  • 2
  • 24
  • 44