3

Is it recommended to create language selector in android app or should I only make the necessary resource files (drawables/strings) for the languages? If someone has the language set to english in his phone but he want to use the app in german he would able to change the language by the selector.

I've tried to implement the selector with this answer but when I closed the app the language reseted to the default. I didn't find any solution to this, so maybe it's not a good idea to make a language selector. Any suggestions?

Thanks!

Community
  • 1
  • 1
daniel.keresztes
  • 875
  • 2
  • 15
  • 25
  • 3
    "Is it recommended to create language selector in android app" - you should ask the UX stack exchange on that one http://ux.stackexchange.com/. – ug_ Dec 01 '14 at 21:22
  • You can use sharedpreferences to remember the language choice of users. – Salih Erikci Dec 01 '14 at 21:23

3 Answers3

2

It's simpler to use the standard approach that relies on the locale set outside the app. But there are good reasons for wanting to switch locales within the app, such as if you were writing a kiosk app that may be used by more than one person.

You can still leverage the built-in resource selection by overriding the locale for the current configuration. But as you've discovered, the locale keeps getting reset to the device default. I've found that if you launch another activity, it inherits the locale of the activity that started it. But if that activity is later recreated, such as on an orientation change, it is recreated with the device's default locale. To maintain your preferred locale, do this in the onCreate(...) of every activity, before calling setContentView(...):

    Resources res = getBaseContext().getResources();
    Configuration config = res.getConfiguration();
    Locale locale = new Locale("es", "US"); // get preferred locale from shared preferences or something
    config.locale = locale; // config.setLocale(...) requires API 17
    res.updateConfiguration(config, res.getDisplayMetrics());
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • It's also worth pointing out that a lot of users may not want to use the language their phone's locale reports it as. A perfect example is Chinese (Traditional, Chinese, Pinyin)! – Jake Lee Nov 26 '16 at 01:33
1

To handle your resetting problem, you should probably use SharedPreferences: http://developer.android.com/reference/android/content/SharedPreferences.html

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("language","value");
  editor.apply();
Bene
  • 724
  • 8
  • 20
  • 1
    Setting the default locale doesn't "stick." It gets changed back to the system default locale whenever one of the app's activities is destroyed or recreated, as on an orientation change. The documentation for `Locale.setDefault(...)` says, "This does not affect system configuration, and attempts to override the system-provided default locale may themselves be overridden by actual changes to the system configuration. Code that calls this method is usually incorrect, and should be fixed by passing the appropriate locale to each locale-sensitive method that's called." – Kevin Krumwiede Dec 01 '14 at 21:36
  • Oh really? I thought it worked like that. Thanks, I corrected that. – Bene Dec 01 '14 at 21:40
1

If you use the localization syntax recommended here:

http://developer.android.com/training/basics/supporting-devices/languages.html

You won't have this issue. If someone is using German as the language on their phone, and you have the localization properly setup, then when you app is loaded, you will have the German language as the correct language. This will also work for any of the languages, and give you the versatility you are looking for.

MyProject/
    res/
       values/
           strings.xml
       values-es/ <---Spanish
           strings.xml
       values-fr/ <---- French
           strings.xml

You will want to make sure that you use the iso-630-1 language codes when creating the values folder.

If you want to give the user the ability to change their language preference on just your app, you could use SharedPreferences to remember their selection, the set the language when the app is loaded/launched.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • I got this part, but if someone using English in his phone, but he wants to use only the app in German then what should I do, if he picks the German from languages then the default language of the app stay on German at the next start? Moreover it's recommended to do this? – daniel.keresztes Dec 01 '14 at 21:38
  • @KERiii, yes, if you use `SharedPreferences`, you will be able to persist the data. Essentially, you'll be able to get the language on just the app, and not the entire phone. You could also use a database, but that is a little overkill. Basically, your issues is that you haven't persisted the data when you kill the app. SharedPreferences will allow you to do this. – BlackHatSamurai Dec 01 '14 at 21:49