The locale is not updated in my library for android app

568 views Asked by At

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

1

There are 1 answers

1
ztan On BEST ANSWER

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() );
 }