Skip to content
Advertisement

Why Java locales are not constants?

Java supports the Locales shown in this link.

But I can’t figure out why only some countries such as France, Canada, China and United States and some languages such as Italian, Japanese and German have constants for their locales.

Everytime a need a locale, I consult the table of supported locales and do the following, using Strings:

Locale locale = new Locale("pt", "BR");

Why only some locales as constants?
Is there a Java library which provide constants for locales?

Advertisement

Answer

Why only some locales as constants?

The javadoc for Locale says this about the constants:

“The Locale class provides a number of convenient constants that you can use to create Locale objects for commonly used locales.”

The obvious implication is that Locale constants have not been defined for other countries and languages because the locales were thought at the time to be “less commonly used” in Java programs. Or at least, not common enough to warrant adding the corresponding constants. (I have no idea if this decision was made based on objective evidence or not.)

Be aware that there would be performance overheads in adding lots more Locale constants. Not to mention all sorts of compatibility issues due to countries changing names, etcetera. So, basically, they had to stop somewhere … and the current set is where they stopped.

The other thing to note is that these constants are merely a convenience; i.e. a simple way to get Locale objects that you can always get by other means; e.g. by using a Locale constructor, or by calling Locale.lookup(...).

Is there a Java library which provide constants for locales?

AFAIK, no. And it seems to me like a bad idea, for the reasons above.

And, of course, you can always define your own class containing Locale constants that are convenient for your applications.


For the record, I found an RFE in the Java Bug Database (in the Google cache) that relates to this:

JDK-4399080 : RFE: java.util.Locale.SPANISH and java.util.Locale.SPAIN does not exist

This RFE was reported against Java 1.3.0, and was closed as “Won’t Fix” with the following Evaluation:

We decided a while ago not to add more predefined locale constants. It’s easy enough to write new Locale("es"); or new Locale("es", "ES"); so there’s no real benefit in adding more constants – especially since it’s not feasible to cover all languages and countries.

norbert.lindenberg@Eng 2001-01-05

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement