0

I get an array of locales from one of our Utility class as below,

enter image description here

Number of unique locales we have as per this list is 2. But it is having same locale twice in 2 letter and 3 letter ISO format. Please help with the logic to get the number of unique locales on this array of Locale.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Parthi
  • 123
  • 1
  • 7
  • Can you please provide some code? – KunLun Aug 17 '21 at 11:41
  • Cannot share the complete part of the code. Using the language code and country codes to form the Locale object as, `Locale(language, country)` where they use language code ***en*** as well as ***eng*** to refer english along with same country code. So, I get two locales on the final list. I want to dedup this locale list to get unique count. – Parthi Aug 17 '21 at 11:47
  • As of now I iterate the locales and using Java Util Locale method getISO3Language to get three letter locale id for all the locales I have and adding it to set. This way I get 3 letter code even for 2 letter locale I am iterating. But there should be some better way, `for (Locale locale : locales) { languages.add(locale.getISO3Language()); }`. Where languages is a set. – Parthi Aug 17 '21 at 11:51
  • When you are looking for duplicate, you are looking just at `language` or even `country`? – KunLun Aug 17 '21 at 11:53
  • only the language. I will have local specific to one country only on this list as shown on the screenshot. Basically looking to get the count of unique languages on the locale list considering both en and eng as same. fr and fra vice versa. – Parthi Aug 17 '21 at 11:58
  • Are you aware that `java.util.Locale` does not support alpha-3 country codes, and also does not support alpha-3 language codes where a alpha-2 code is available? In any case, [this](https://stackoverflow.com/questions/62982135/convert-language-code-three-characters-iso-639-2-to-two-character-code-iso-63) might be helpful. – Sweeper Aug 17 '21 at 11:59
  • 1
    So `Locale` doesn't even recognise what locale fra-CAN represents (try printing its display name). `getISO3Language` has this special property that "If the locale specifies a three-letter language, the language is returned as is", which is what makes your solution work. This as far as I know is the only API in `Locale` that knows about the alpha-3 language codes that also have alpha-2 counterparts. I don't think you are going to get better than that. That said, what sort of "better" way are you looking for? – Sweeper Aug 17 '21 at 12:07
  • 1
    What about this: `List distLangs = localList.stream().map(Locale::getISO3Language).distinct().collect(Collectors.toList()); System.out.println(distLangs);` ? But I'm still not sure I understood your problem. – KunLun Aug 17 '21 at 12:13
  • I am doing similar thing with for loop. But this looks like a better way. I will get the count instead of collecting it. count of distinct languages is what I am looking for. Java utility to get the unique locale's would be good. Thanks – Parthi Aug 17 '21 at 12:42

0 Answers0