4

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);
Community
  • 1
  • 1
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
  • 1
    What are you trying to do? Are you planning to change the language within your application? – ChuongPham Dec 24 '14 at 02:59
  • Yes. I would like to reduce the number of necessary clicks. – Rodrigo Dec 24 '14 at 03:04
  • The clicks won't be a problem. The problem, however, is which languages your app will support. Remember that if you have a list of languages from which a user can select, you must also create the necessary `values` directory within your Android project that holds the translated text for the languages you want to support. Getting languages from `Locale.getAvailableLocales()` will cause errors if a particular language has not been translated or is missing from your Android application. So, please edit your post and outline what you want exactly. – ChuongPham Dec 24 '14 at 03:10
  • OK, thank you. I added a first paragraph. Since it's for personal use, I'm interested right now in portuguese, english, spanish and chinese. – Rodrigo Dec 24 '14 at 03:16
  • I'm unclear with your recent edit as to your user/business requirement, not your technical requirement i.e. I know what you can get from `Locale.getAvailableLocales()` method, but I don't know what you want to do from a user/business perspective. Anyway, I have provided an example in my answer below. Hope it helps to explain your actual user/business requirements. – ChuongPham Dec 24 '14 at 03:21
  • If I choose Bahasa Indonesia, a lot of things are translated to Bahasa Indonesia. This is useful to study Bahasa Indonesia. I would like to do this via my app, just to save some 8 or 9 clicks. And I will know that the system has a lot to say of Bahasa Indonesia because it is on the list on the image. I'll show 2 more images, so you'll see the differences in my device. – Rodrigo Dec 24 '14 at 04:04

2 Answers2

2

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

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
2

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)
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • I don't want static lists. I want to programmatically get the language names exactly as they appear on the image, i.e. accessing Quick settings > Settings > Language & input > Language. So the user will select which of these languages he/she want to put in a list to switch from. – Rodrigo Dec 24 '14 at 03:26
  • Like I said before, will you be supporting all possible combination of spoken languages (there are over 260 languages)? If not, then using the `Locale.getAvailableLocales()` method, which is equivalent to getting the languages from **Quick settings > Settings > Language & input > Language** will not help you. Your app will crash when a user selects a language for which you don't have a corresponding translation file (`strings.xml`) in your Android project. – ChuongPham Dec 24 '14 at 03:30
  • I have around 60 languages in my Sony Xperia menu, and I can choose any of them and the system language changes. That's all I want to do. The list of languages comes from some file inside the phone. Isn't it open-source? How can't I find and copy the list? I will accept to exhibit XXXX or ____ for anything my project don't have the proper files. But "English (US)", "Bahasa Indonesia"? I think I could show these. – Rodrigo Dec 24 '14 at 03:50
  • Both lists look very different. Locale.getAvailableLocales() is giving me far more options. – Rodrigo Dec 24 '14 at 03:51
  • That's why I suggest you _not_ to use `Locale.getAvailableLocales()` method. If you do, and you don't have the translation (`strings.xml`) for a given language, then your app simply just crashed, or worst, you or your users select Indonesian and your app displays English instead (because there's no support for Indonesian in your app). The factory Android firmware of your Sony Xperia would have all the translations included in it. If not, then either Google or Sony is not doing a good job of testing their Android firmware for internationalisation (i18n) support. – ChuongPham Dec 24 '14 at 03:56
  • Another thing: a Sony Xperia Android device sold in South America will have a different sets of languages to one sold in Europe, Asia, or even North America. So, relying on the `Locale.getAvailableLocales()` method to give your application a list of support languages is a disaster waiting to happen. Users tend to uninstall apps pretty quick if they don't work as advertised. – ChuongPham Dec 24 '14 at 04:07
  • So Locale.getAvailableLocales() is not what I'm looking for. It is probably some system files, but I'm really new to Android. I added 2 images to show what I thought it could be. – Rodrigo Dec 24 '14 at 04:09
  • 2
    System internationalisation files, ROM (i.e Android firmware) customisation and extraction is beyond the scope of this post. I would not recommend going down this path for new Android developers since the process is quite complicated. Try to implement my answer for the languages you want to support and see how you go. That's one way to understand how Android internationalisation works. – ChuongPham Dec 24 '14 at 04:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67590/discussion-between-rodrigo-and-chuongpham). – Rodrigo Dec 24 '14 at 04:48