- Using ICU4J Locale Service Provider
- Optional Configuration
Java SE 6 introduced a new feature which allows Java user code to extend locale support in Java runtime environment. JREs shipped by Oracle or IBM come with decent locale coverage, but some users may want more locale support. Java SE 6 includes abstract classes extending
java.util.spi.LocaleServiceProvider. Java SE 6 users can create a subclass of these abstract class to supply their own locale support for text break, collation, date/number formatting or providing translations for currency, locale and time zone names.
ICU4J has been providing more comprehensive locale coverage than standard JREs. However, Java programmers have to use ICU4J’s own internationalization service APIs (
com.ibm.icu.\*) to utilize the rich locale support. Sometimes, the migration is not an option for various reasons. For example, your code may depend on existing Java libraries utilizing JDK internationalization service APIs, but you have no access to the source code. In this case, it is not possible to modify the libraries to use ICU4J APIs.
ICU4J Locale Service Provider is a component consists of classes implementing the Java SE 6 locale sensitive service provider interfaces. Available service providers are:
ICU4J Locale Service Provider is designed to work as installed extensions in a JRE. Once the component is configured properly, Java application running on the JRE automatically picks the ICU4J’s internationalization service implementation when a requested locale is not available in the JRE.
Java SE 6 locale sensitive service providers are using the Java Extension Mechanism. An implementation of a locale sensitive service provider is installed as an optional package to extend the functionality of the Java core platform. To install an optional package, its JAR files must be placed in the Java extension directory. The standard location is *
java -Djava.ext.dirs=%JAVA_HOME%\\lib\\ext;%ICU_SPI_DIR% <your_java_app>[Microsoft Windows]
java -Djava.ext.dirs=$JAVA_HOME/lib/ext:$ICU_SPI_DIR <your_java_app>[Linux,Solaris and other unix like platforms]
The ICU4J’s implementations of Java SE 6 locale sensitive service provider interfaces and configuration files are packaged in a single JAR file (*icu4j-localespi-
Once the ICU4J Locale Service Provider is installed properly, factory methods in JDK internationalization classes look for the implementation provided by ICU4J when a requested locale is not supported by the JDK service class. For example, locale af_ZA (Afrikaans - South Africa) is not supported by JDK
DateFormat in Oracle Java SE 6. The following code snippet returns an instance of
DateFormat from ICU4J Locale Service Provider and prints out the current date localized for af_ZA.
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, new Locale("af", "ZA")); System.out.println(df.format(new Date()));
2008 Junie 19[With ICU4J Locale Service Provider enabled]
June 19, 2008[Without ICU4J Locale Service Provider]
By default, all Java 6 SE locale sensitive service providers are enabled in the ICU4J Locale Service Provider JAR file. If you want to disable specific providers supported by ICU4J, you can remove the corresponding provider configuration files from META-INF/services in the localespi JAR file. For example, if you do not want to use ICU’s time zone name service at all, you can remove the file: META-INF/services/java.util.spi.TimeZoneNameProvider from the JAR file.
DateFormatSymbolsProvider/DecimalFormatSymbolsProvider won’t affect the localized symbols actually used by
DateFormatProvider/NumberFormatProvider by the current implementation. These services are implemented independently.
com/ibm/icu/impl/javaspi/ICULocaleServiceProviderConfig.properties in the localespi JAR file is used for configuring the behavior of the ICU4J Locale Service Provider implementation. There are some configuration properties available. See the table below for each configuration in detail.
| || || ||Whether if Locales with ICU’s variant suffix will be included in |
| ||Any String|| ||Suffix string used in Locale’s variant field to specify the ICU implementation.|
| || || ||Whether if 3-letter language locales are included in |
| || || ||Whether if |