2

I want to get list of languages under Language and input settings. i am already able to get list of languages supported by device but that is a huge list.This is my code to get the list but gives me a huge list.I want to display only the languages shown under Language and input settings.

 Locale[] locales = Locale.getAvailableLocales();
        ArrayList<String> localcountries=new ArrayList<String>();
        for(Locale l:locales)
        {
            localcountries.add(l.getDisplayLanguage().toString());
        }
String[] languages=(String[]) localcountries.toArray(new String[localcountries.size()]);
tanmeet
  • 220
  • 2
  • 11
  • Sorry I don't understand you, could you be more explicit please? What do you expect to get? – M. Mariscal May 12 '16 at 09:24
  • yes sure,Now when you go in your device setting you see the setting language and input inside that you have language on click of which you will get list of languages. – tanmeet May 12 '16 at 09:25
  • I wish to get the same list in my apps – tanmeet May 12 '16 at 09:26
  • i am ok to use reflection also if it is possible that way. – tanmeet May 12 '16 at 09:26
  • So use hashset to get non-duplicate languages, I saw that it repeats Arabic, and some languages... @Maher Abuthraa is right, he does what you expected, he also gives you simple code cleaner – M. Mariscal May 12 '16 at 09:27
  • My objective is not to remove the duplicates.As i mentioned i want to get the same list that i get under language and input settings when i set my device language.if you go in your device settings even english comes many times.So using hashset is not the solution. I again say my objective to get the same languages that are displayed in device setting under language and input settings – tanmeet May 12 '16 at 09:35
  • have you got the solution..? @tanmeet – Ramachandran A May 17 '18 at 10:51

3 Answers3

0

Just use LinkedHashSet to insert languages .. by the end you will get all languages without duplications

LinkedHashSet<String> hashSet = new LinkedHashSet<String>();
            Locale[] locales = Locale.getAvailableLocales();
            for(Locale l:locales)
            {
                hashSet.add(l.getDisplayLanguage());
            }

Good luck,'.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • My objective is not to remove the duplicates.As i mentioned i want to get the same list that i get under language and input settings when i set my device language.if you go in your device settings even english comes many times.So using hashset is not the solution. I again say my objective to get the same languages that are displayed in device setting under language and input settings. – tanmeet May 12 '16 at 09:35
0

I think that you are looking for something like this.. hope it be helpful my friend ;)

Locale[] locales = Locale.getAvailableLocales();
    ArrayList<String> localCountries = new ArrayList<String>();
    for(Locale l:locales)
    {
        localCountries.add(l.getDisplayLanguage() + " (" + l.getDisplayCountry() + ")");
    }
System.out.println(localCountries.subList(1, localCountries.size())); 
M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
  • No brother i am not finding this.This will give me just lang and country what i want is list of langauges that are displayed under langauge and input settings.Check your device settings once. – tanmeet May 12 '16 at 10:15
  • Oh my god... http://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android hope it be what you are looking for... – M. Mariscal May 12 '16 at 10:20
  • No i was not looking for that.But i have accomplished what i needed after lot of brain hamering. – tanmeet May 12 '16 at 11:15
0

Finally i got what i was looking for after lot of brain hamering.this gives me list of langauges that are displayed under langauge and input settings

 public void getLangs()
    {
        String[] systemLocaleIetfLanguageTags = getAssets().getLocales();
        Arrays.sort(systemLocaleIetfLanguageTags);
        this.data = new ArrayList();
        for (String ietfLanguageTag : systemLocaleIetfLanguageTags)
        {
            if (ietfLanguageTag != null && ietfLanguageTag.length() == 5)
            {
                this.data.add(new Loc(ietfLanguageTag));
            }
        }
    }

also create a class to get all languages

public class Loc {
    protected Locale locale;

    public Loc(String ietfLanguageTag) {
        this.locale = null;
        String[] l = ietfLanguageTag.split("_");
        if (l.length == 2) {
            this.locale = new Locale(l[0], l[1]);
        }
    }

    public String oneLineLanguageCountry() {
        return String.format("%s (%s)", new Object[]{toTitleCase(this.locale.getDisplayLanguage(this.locale)), toTitleCase(this.locale.getDisplayCountry(this.locale))});
    }

    public String twoLinesLanguageCountry() {
        return String.format("%s\n%s", new Object[]{toTitleCase(this.locale.getDisplayLanguage(this.locale)), toTitleCase(this.locale.getDisplayCountry(this.locale))});
    }

    public String getIetfLanguageTag() {
        return String.format("%s_%s", new Object[]{this.locale.getLanguage(), this.locale.getCountry()});
    }

    public Locale getLocale() {
        return this.locale;
    }

    private static String toTitleCase(String s) {
        return s.length() == 0 ? s : new StringBuilder(String.valueOf(Character.toUpperCase(s.charAt(0)))).append(s.substring(1)).toString();
    }

    public boolean isLocaleCurrentLocale() {
        return Locale.getDefault().equals(getLocale());
    }

    public String toString() {
        return oneLineLanguageCountry();
    }
}

please vote my answer if it worked for you too

tanmeet
  • 220
  • 2
  • 11