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!
If you ever need to change the value of a String constant, it is a one-line change. With an enum it can get more complicated due to having separate values for name and toString, and those values possibly being used in conditional logic.
The Java Locale class object represents a specific geographic, cultural, or political region. It is a mechanism to for identifying objects, not a container for the objects themselves. A Locale object logically consists of the fields like languages, script, country, variant, extensions.
To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With