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?

alfonx
  • 6,936
  • 2
  • 49
  • 58

2 Answers2

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.

jenaiz
  • 547
  • 2
  • 15
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • _"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."_ - did you miss the bit you quoted about installing a `LocaleServiceProvider`? – Matt Ball Jan 17 '11 at 18:02
  • I agree it's a shortcut. But since the OP didn't explicitely explain what he meant by "extending the list of available locales", I suspected that what he wanted just needed him to instantiate a new one. Using ResourceBundle doesn't need anything other than a new Locale. Number and date formatting could reuse the rules from another language. He would then only have to use a new country code or variant code. – JB Nizet Jan 17 '11 at 18:47
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.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thanks. Sadly manipulating every users JRE is not an option for my application: "Since the Locale Sensitive Services SPIs are based on the standard Java Extension Mechanism, you can package them as a JAR file (with a few tricks in its MANIFEST file, which can be found in here) and place it in the extension directory." – alfonx Jan 17 '11 at 18:00