Alex Rivera | Logout

How to extend the list of available Java Locales

Asked 2011-01-17T17:48:52.587
9

I am looking for a way to add more Locales to the Locales available in Java 1.6. But the Locales I want to create do not have ISO-3166 country codes, nor ISO-639 language codes. Is there any way to do this anyways? The Locales I want to add only differ in the language names, but the smaller an ethnic group is, the more picky they get about their identity ;-)

So I thought about extending an existing Locale, something like

UserDefinedLocale extends Locale { 
   UserDefinedLocale (Locale parentLocale) {...}
}

but java.util.Locale is final, which makes it especially hard to hack something around...

So, is the idea that the list of Java Locales is exhaustive? Am I the first to miss some more Locales?

Edit
Report

2 Answers

6

Read the javadoc for java.util Locale.

It says : "Create a Locale object using the constructors in this class: "

It also says : "Because a Locale object is just an identifier for a region, no validity check is performed when you construct a Locale"

It also says : "A Locale is the mechanism for identifying the kind of object (NumberFormat) that you would like to get. The locale is just a mechanism for identifying objects, not a container for the objects themselves"

And finally, the javadoc for the getAvailableLocales() method says : "The returned array represents the union of locales supported by the Java runtime environment and by installed LocaleServiceProvider implementations"

So you just have to invent a language code which is not in the standard list, and use it as an identifier for your locale.

answered 2011-01-17T18:00:09.430
3

See this answer:

...You can plug in support for additional locales via the SPIs (described here). For example, to provide a date formatter for a new locale, you would do it by implementing a DateFormatProvider service. You might be able to do this by decorating an existing implementation - I'd have a look at the ICU4J library to see if it provides the support you want.

answered 2011-01-17T17:53:54.523

Your Answer