获取指定语言的国家/地区列表

use*_*993 1 java country-codes

我已经可以获取国家/地区列表,但是名称用我的语言(意大利语)显示。我需要将它们用英语显示,这就是我现在所拥有的(此代码不是我的,我是从网络上复制的)

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);
Run Code Online (Sandbox Code Playgroud)

谢谢=)

Ell*_*sch 5

您应该使用指定输出语言环境,Locale.getDisplayCountry(Locale)例如

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));
Run Code Online (Sandbox Code Playgroud)