3

I am trying to list out the available Locales as follows.

private List<String> getInstalledLanguages() {
    Locale[] listLocales = Locale.getAvailableLocales();
    List<String> listInstalledLanguages = new ArrayList<String>();
    for(int i=0; i<listLocales.length; i++) {
        String language = listLocales[i].getDisplayLanguage();
        Log.i(TAG, "Language : "+language);
        if (language.equals("English") && !listInstalledLanguages.contains("English"))
            listInstalledLanguages.add(language);

        if (language.equals("Hindi") && !listInstalledLanguages.contains("Hindi"))
            listInstalledLanguages.add(language);

        if (language.equals("Kannada") && !listInstalledLanguages.contains("Kannada"))
            listInstalledLanguages.add(language);
    }

    return listInstalledLanguages;
}

In Samsung Tab3, I can see that Hindi and Kannada languages are listed in the Language settings but when I run the above code, I don't see them in list with the other Languages (in Log).

Hindi and Kannada languages are listed in language settings in their respective language. I mean, Hindi is listed as हिन्दी and Kannada as ಕನ್ನಡ.

Even I tried to get the Locales in their respective regional names (हिन्दी and ಕನ್ನಡ as it is) as follows but could not succeed.

if (language.equals("हिन्दी"))
       listInstalledLanguages.add(language);

can anyone please help me out?

TIA.

Braj
  • 2,164
  • 2
  • 26
  • 44

2 Answers2

1

Based on comments and other answers, I understand that you couldn't see languages "Hindi" and "Kannada" in your log and neither in your list, but you can see them in your phone settings. I thinkthat you should try to changhe your getAvaiableLocales because as Android's Documentation states:

Most locale-sensitive classes offer their own getAvailableLocales method, which should be preferred over this general purpose method.

You should also change your listLocales[i].getDisplayLanguage(); to listLocales[i].getDisplayLanguage(Locale.US); to be sure to obtain the language name in English

Matteo Corti
  • 484
  • 2
  • 13
  • `Locale.US` solved the issue. Now I am able to get required languages. Sorry @Matteo, you missed bounty because of my late reply :( – Braj Sep 18 '14 at 05:35
0

Based on comments, I understand/assume the following:

  1. The code Log.i(TAG, "Language : "+language); prints Hindi as हिन्दी and Kannada as ಕನ್ನಡ.
  2. But listInstalledLanguages does not contain the two languages.
  3. The above issue occurs only in Samsung Tab3 which lists languages as UNICODE and not other models.

This might be because of following reason: String Comparison with unicode

You probably need to compare strings using collator which is documented as below:

The java.text.Collator class provides linguistic comparisons. It's not as fast as String's compareTo, but it is supposed to be correct for linguistic comparisons. If correctness in that situation is important to you, you have to use this class.

So, try using a collator with locale HINDI and KANNADA and compare strings, in addition to the regular string comparison so that it will work in all models.

Code:

if (language.equals("Hindi") || collator.compare(language, "हिन्दी"))
{
    listInstalledLanguages.add(language);
}
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • Thank u for the response. Log is `not` printing those 2 Languages. Other than Hindi and Kannada, I can see all other Languages. Anyway, I will try ur answer once. – Braj Sep 09 '14 at 06:49
  • @Braj: I asked if log is printing and you said yes?? and so I posted this answer. Anyway let this be here since it might help someone who wants to do unicode comparison – ngrashia Sep 09 '14 at 06:50
  • Sorry for that. I was confused :D – Braj Sep 09 '14 at 06:51