Regional languages ​​Language is available but not displayed in the log

I am trying to list 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 the languages ​​are Hindi

and Kannada

are listed in the language settings, but when I run the above code, I don't see them listed with other languages ​​(in the log).

Languages

Hindi

and Kannada

are listed in the language setting in the respective language. I mean Hindi is listed as हिन्दी

and Kannada as ಕನ್ನಡ

.

Even I tried to get locales in my regional names (हिन्दी and ಕನ್ನಡ as is) in the following way, but couldn't succeed.

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

      

Can anyone help me?

TIA.

+3


source to share


2 answers


Based on the comments and other answers, I understand that you could not see the Hindi and Kannada languages ​​in your journal or your list, but you can see them in your phone settings. I think you should try to change your getAvaiableLocales because the Android documentation states:

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



You must also change your listLocales[i].getDisplayLanguage();

to listLocales[i].getDisplayLanguage(Locale.US);

to get the language name in English

+1


source


Based on the comments, I understand / assume the following:

  • The code Log.i(TAG, "Language : "+language);

    prints Hindi as हिन्दी and Kannada as ಕನ್ನಡ.
  • But listInstalledLanguages

    does not contain two languages.
  • The above problem only occurs on Samsung Tab3 which lists languages ​​as UNICODE and not other models.

This may be due to the following reason: String Comparison with unicode

You probably need to compare strings using the collator , which is described below:



The java.text.Collator class provides linguistic comparisons. Not as fast as String compareTo, but it should be correct for linguistic comparisons. If correctness is important to you in this situation, you should use this class.

So, try using collator with the HINDI and KANNADA languages ​​and compare strings in addition to regular string matching so that it works in all models.

Code

if (language.equals("Hindi") || collator.compare(language, "हिन्दी"))
{
    listInstalledLanguages.add(language);
}

      

0


source







All Articles