Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of full language names in Java

Tags:

java

How can I get a list of full names of languages? I checked Locale class and found getISOLanguages(), but it returns 2-letter language codes (for instance, 'en') when I need full name of language (for example, 'English').

What is the better approach for that?

like image 649
Gleb Avatar asked Jan 16 '17 15:01

Gleb


1 Answers

Finally, thanks to Thomas comments I implemented it in such way:

SortedSet<String> allLanguages = new TreeSet<String>();
String[] languages = Locale.getISOLanguages();
for (int i = 0; i < languages.length; i++){
    Locale loc = new Locale(languages[i]);
    allLanguages.add(loc.getDisplayLanguage());
}

UPD. Also there is the more modern style:

Set<String> languages = Arrays.stream(Locale.getISOLanguages())
            .map(Locale::new)
            .map(Locale::getDisplayLanguage)
            .collect(Collectors.toCollection(TreeSet::new));
like image 174
Gleb Avatar answered Oct 18 '22 10:10

Gleb