25

I am working on globalization of Android app. I have to provide options to choose different locales from within the app. I am using the following code in my activity (HomeActivity) where I provide option to change the locale

Configuration config = new Configuration();
config.locale = selectedLocale; // set accordingly 
// eg. if Hindi then selectedLocale = new Locale("hi");
Locale.setDefault(selectedLocale); // has no effect
Resources res = getApplicationContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());

This works fine as long as there are no configuration changes like screen rotation where the locale defaults to the android system level locale rather than the locale set by the code.

Locale.setDefault(selectedLocale);

One solution I can think of is to persist the user selected locale using SharedPreferences and in each activity's onCreate() method have the locale set to the persisted locale as the onCreate() gets called again and again for every configuration change. Is there any better way to do this so that I don't have to do it in every activity.

Basically what I want is that - Once I change/set to some locale in my HomeActivity I want all the activities within my app to use that locale itself irrespective of any configuration changes....unless and until it is changed to other locale from the app's HomeActivity that provides options to change locale.

Julien Palard
  • 8,736
  • 2
  • 37
  • 44
Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40

1 Answers1

29

Although the solution stated in this answer works in the general case, I found myself adding to my preference screen:

 <activity android:name="com.example.UserPreferences"
     android:screenOrientation="landscape"
     android:configChanges="orientation|keyboardHidden|screenSize">
 </activity>

This is because when the application is in landscape mode and the preference screen in portrait mode, changing the locale and going back to the application might cause trouble. Setting both to be in landscape mode prevent this from happening.

General solution

You need to change the locale at the Application level, so that its effects are everywhere.

public class MyApplication extends Application
{
  @Override
  public void onCreate()
  {
    updateLanguage(this);
    super.onCreate();
  }

  public static void updateLanguage(Context ctx)
  {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String lang = prefs.getString("locale_override", "");
    updateLanguage(ctx, lang);
  }

  public static void updateLanguage(Context ctx, String lang)
  {
    Configuration cfg = new Configuration();
    if (!TextUtils.isEmpty(lang))
      cfg.locale = new Locale(lang);
    else
      cfg.locale = Locale.getDefault();

    ctx.getResources().updateConfiguration(cfg, null);
  }
}

Then in your manifest you have to write:

<application android:name="com.example.MyApplication" ...>
  ...
</application>
Aki Koskinen
  • 142
  • 1
  • 8
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
  • If I used English language. I will call this `updateLanguage(this, "en");` that's right? Or try with `updateLanguage(this, Locale.US.getDisplayLanguage());` Not worked for me. – Huy Tower Jun 20 '14 at 09:29
  • the string "locale_override" should be replaced by whatever key your preference is pointing to the language. – Mikaël Mayer Jun 20 '14 at 10:14
  • It's a nice solution. BUT unfortunately, it only works on my Galaxy S4 running Android 4.4.2 when I'm debugging. When running normal, it does not change the language, but produces a lot of additional output in LogCat. The solution is to call the static method updateLanguage( ctx ) in the onCreate method of my main activity, and not in the onCreate of the application. But in that case I don't need to derive class Application. – infero Mar 27 '15 at 10:35
  • 2
    Note that `updateConfiguration()` is deprecated. Alternatives are suggested in [Set Locale programmatically](https://stackoverflow.com/a/47223492/905686). – user905686 Nov 20 '17 at 16:33
  • Is there is a reason to define `updateConfiguration()` as `static` ? @MikaëlMayer – Saleh Apr 19 '19 at 13:42
  • I don't see any `updateConfiguration` defined here. If you talk about "updateLanguage", the only reason I define `updateLanguage` as static is that it does not depend on the class itself, so there is no reason to attach it as a "method" to the class. Things would be different if the context or the language were members of the class. – Mikaël Mayer Apr 23 '19 at 14:07