Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend the list of available Java Locales

Tags:

java

locale

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?

like image 838
alfonx Avatar asked Jan 17 '11 17:01

alfonx


2 Answers

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.

like image 164
JB Nizet Avatar answered Dec 05 '22 07:12

JB Nizet


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.

like image 42
Matt Ball Avatar answered Dec 05 '22 06:12

Matt Ball