Get a country list in a determinated language

1.2k Views Asked by At

I'm already able to get a list of countries, but the name are desplayed in my language (italian). I need them to be desplayed in english, this is what I have right now (This code is not mine, I copied it from the web)

String[] locales = Locale.getISOCountries();
    ArrayList<String> list = new ArrayList<String>(500);
    for (String countryCode : locales) {

        Locale obj = new Locale("en", countryCode);
        list.add(obj.getDisplayCountry());
    }
    Collections.sort(list);
    country = new String[list.size()];
    country = list.toArray(country);

Thank you =)

1

There are 1 best solutions below

0
On

You should specify the output locale with Locale.getDisplayCountry(Locale) and something like

String[] locales = Locale.getISOCountries();
List<String> list = new ArrayList<>(500); // <-- List interface, and diamond
                                          //     operator.
Locale outLocale = new Locale("EN", "us"); // <-- English US
for (String countryCode : locales) {
    Locale obj = new Locale("en-us", countryCode);
    list.add(obj.getDisplayCountry(outLocale));
}
Collections.sort(list);
String[] country = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(country));