How to list/select the installed languages on Android?

3k Views Asked by At

I want to make a small app that, with one or two clicks, will switch the system language between two, three or more prefered languages, selected before-hand from the "official" language list, i.e. the one that appears in Quick settings > Settings > Language & input > Language.

How do I get this list programatically? Is this the locale list, available from Locale.getAvailableLocales()? Should I get to the languages from the locales? Thank you!

enter image description here

EDIT - This is what I've done so far:

  private List<Model> getModel() {
    Locale[] locales = Locale.getAvailableLocales();
    List<Model> list = new ArrayList<Model>();
    for (int i=0; i<locales.length; i++) {
        list.add(get(locales[i].getDisplayLanguage()));
    }
    return list;
  }

  private Model get(String s) {
    return new Model(s);
  }

I'm looking those options now:

Locale.getDefault().getLanguage()        ---> en     
Locale.getDefault().getISO3Language()    ---> eng
Locale.getDefault().getCountry()         ---> US
Locale.getDefault().getISO3Country()     ---> USA
Locale.getDefault().toString()           ---> en_US
Locale.getDefault().getDisplayLanguage() ---> English
Locale.getDefault().getDisplayCountry()  ---> United States
Locale.getDefault().getDisplayName()     ---> English (United States)

from here.

EDIT 2 - 2 more images, showing the end of the languages list, where reside the options for chinese.

enter image description here

This is the list from the system.

enter image description here

This list was done with the following code:

Locale[] locales = Locale.getAvailableLocales();
List<Model> list = new ArrayList<Model>();
for (int i=0; i<locales.length; i++) {
    list.add(get(locales[i].getDisplayLanguage()+"|"+
            locales[i].toString()+"|"+
            locales[i].getDisplayName()
            ));
}
return list;

Now there must be a way to get to the first list from the second list, perhaps accessing some system hidden files?

EDIT 3 - I added the following code and it shows the chinese, portuguese and spanish special characters (UTF-8) without problem:

list.add(get("Português (Brasil)"));
list.add(get("English (US)"));
list.add(get("中国(简体)"));
list.add(get("Español (Venezuela)"));

Now how do I get from here to actually change the system language (without opening the intent/activity like below?

Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(intent);
2

There are 2 best solutions below

0
On

Here is the API to get all the available locales of your device.

public static Locale[] getAvailableLocales ()

For more information please check this public link : http://developer.android.com/reference/java/util/Locale.html#getAvailableLocales()

9
On

If you planned to support all possible combination of languages out there, then Locale.getAvailableLocales() might work for you.

If not, then you'll need to specifically provide a list of languages yourself within your Android application. For example, if your app supports Catalan, Czech, Danish and German (or the many flavours of spoken German), you would be better off populating your ListView with a static String list:

public static final String[] LANGS = new String[] {"English (Australia)", "Català (Espanya)","Čeština (Česká Republika)","Dansk (Danmark)","Deutsch (Österreich)","Deutsch (Belgien)","Deutsch (Schweiz)","Deutsch (Deutschland)","Deutsch (Liechtenstein)","Deutsch (Luxemburg)"};

public static final String[] LANG_CODES = new String[] {"en_au", "ca_ES","cs_CZ","da_DK","de_AT","de_BE","de_CH","de_DE","de_LI","de_LU"};

And, in your Android project, you'll have the following values directories to support the above languages:

/res/values (English)
/res/values-ca (Catalan)
/res/values-cs (Czech)
/res/values-da (Danish)
/res/values-de (German)