-1

I have a service which gives the language display name as part of response. I want to retrieve the language code so that I can apply the internationalization for output file.

Expected input : English - United States
Output : en_US
Soumyajit Swain
  • 1,298
  • 1
  • 21
  • 35
  • 1
    You can loop over the [available locales](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#getAvailableLocales--) and look for a match. – Holger Mar 14 '17 at 10:53
  • 1
    please see answers for this question - http://stackoverflow.com/questions/14155049/iso2-country-code-from-country-name – Anton Balaniuc Mar 14 '17 at 15:16

1 Answers1

1

You might use something like that:

Optional<Locale> locale = Arrays.stream(Locale.getAvailableLocales())
        .filter(l ->
                l.getDisplayLanguage().equals("English") &&
                l.getDisplayCountry().equals("United States")
        ).findAny();

locale.ifPresent(System.out::println);
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53