Question

Are the IETF BCP 47 language tags defined as enums anywhere in JDK?

For Locale.forLanguageTag() we pass values like fr-FR, jp-JP etc.

Are there any enums already provided by JDK for it? Or should the developer be writing a custom enum for that? How to handle it in i18n application?

Was it helpful?

Solution

Enum would be wrong way for this in JDK API which is supposed to serve wide variety of applications.

It would be also wrong in application development, unless the application is purposedly designed to serve a fixed, limited subset of languages - subset that is known at compile time - which, in turn, would hardly qualify as related to BCP-47.

This is because substantial attribute of BCP-47 is addressing an open-ended set of values:

Language tags are used to help identify languages, whether spoken, written, signed, or otherwise signaled, for the purpose of communication. This includes constructed and artificial languages but excludes languages not intended primarily for human communication, such as programming languages...

The ietf-languages list is an open list...

Above, in turn, makes it wrong fit for enum, which is intended to serve fixed, limited sets of values:

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time - for example, the choices on a menu, command line flags, and so on...

OTHER TIPS

JDK does not contain an enum for IETF BCP 47 language tags. Inside Oracle JDK they are expressed inside a non-public string array of a sun.util.* class. You should not get them from there.

In Java 7 you can iterate over available locales and call toLanguageTag() for each one or, as you say, create your own enum.

Licensed under: CC-BY-SA with attribution
scroll top