11

I'd like to be able to reference languages in a standardized way (ISO). Is there an existing enumeration as part of java or a common dependency such as Apache that I can reuse rather than having to implement my own?

To emphasize, I'm not looking for countries, but for languages!

Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36
  • The static instances of `Locale`? –  Dec 14 '18 at 12:26
  • 2
    What about [Is there an open source java enum of ISO 3166-1 country codes](https://stackoverflow.com/questions/139867/is-there-an-open-source-java-enum-of-iso-3166-1-country-codes)? – Druckles Dec 14 '18 at 12:27
  • 1
    That refers to country codes, I'm looking for something ISO 639 Language codes. – Andreas Hartmann Dec 14 '18 at 12:27
  • 1
    `Locale.getAvailableLocales()` will give you a list of all supported locales, and you can use the methods available in class `Locale` to get the associated language and country codes. Specifically, [`Locale.getISOLanguages()`](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#getISOLanguages--) gives you ISO 639 language codes. – Jesper Dec 14 '18 at 12:29
  • getAvailableLocales doesn't cover ISO 639 – Andreas Hartmann Dec 14 '18 at 12:34
  • Would this work? https://github.com/TakahikoKawasaki/nv-i18n – brunobastosg Dec 14 '18 at 13:01

1 Answers1

7

How about the Locale.getISOLanguages() which covers ISO 639. However, it's not an enum but the String array (String[]).

Returns a list of all 2-letter language codes defined in ISO 639. Can be used to create Locales.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Not bad, didn't know that one, however an enum would be better. – Andreas Hartmann Dec 14 '18 at 12:36
  • Also: "ISO 639 is not a stable standard— some languages' codes have changed. The list this function returns includes both the new and the old codes for the languages whose codes have changed." so some languages are duplicated here. – Andreas Hartmann Dec 14 '18 at 12:37
  • `I'd like to be able to reference languages in a standardized way (ISO)` what **ISO** do you mean? – Nikolas Charalambidis Dec 14 '18 at 12:43
  • Any of the versions of ISO-639 without duplicates. – Andreas Hartmann Dec 14 '18 at 12:50
  • There are `188` elements in the returned array (`jdk 1.8.0_172`) and none of them is duplicated. What exactly do you mean? Could you provide an example? – Nikolas Charalambidis Dec 14 '18 at 12:57
  • According to https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Locale.html#getISOLanguages() Some languages are included multiple times, but with different codes. This may or may not be an issue depending on the use case. – Andreas Hartmann Dec 14 '18 at 17:29
  • So basically this method merges together different version of the ISO standard. And since it's an array that can also change in the future, addressing a specific language may be problematic. Additionally for some reason this method at every single call at runtime it splits its internal String into an array rather than just having the array hard coded, or better yet, be an enum. So while your answer is sort of correct, it's not really great either. – Andreas Hartmann Dec 14 '18 at 17:32
  • It returns all the currently recognized codes. You might want to find a 3rd party ic API for this use-case. – Nikolas Charalambidis Dec 14 '18 at 18:12