简体   繁体   中英

how to get the Locale list in en_<COUNTRY> format

I want to return a list of all the countries in <lang>_<Country> format where <lang> should be english, So I need a list like en_GB,en_US,en_JP etc. Looking at the Locale's api is returning <lang>_<Country> but it is not restricting it to language as en. Am I missing something here?

Try

    Locale[] availableLocales = Locale.getAvailableLocales();
    for (Locale locale : availableLocales) {
        if ("en".equals(locale.getLanguage())) {
            System.out.println(locale);
        }
    }

you can filter the list to only include en as the language.

note : some Locales have a language but no country

How about:

        Locale[] allLocales = Locale.getAvailableLocales();
        String desiredLanguage="en";
        List<Locale> desiredLocales = new ArrayList<Locale>();
        for(Locale locale:allLocales) {
            if(locale.getLanguage().equals(desiredLanguage)) {
                desiredLocales.add(locale);
                System.out.println("added: "+locale);
            }
        }

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM