Locale-sensitive Operations and Services in International Java Applications | 2 | WebReference

Locale-sensitive Operations and Services in International Java Applications | 2


[prev]

Locale-sensitive Operations and Services in International Java Applications [con't]

The Service Provider Interface (SPI) enables the plugin of locale-dependent data and services by third parties so that they can provide implementations of most locale-sensitive classes in the java.text and java.util packages.

The implementation of SPIs is based on abstract classes and Java interfaces that are implemented by the service provider. At runtime the Java class-loading mechanism is used to dynamically locate and load classes that implement the SPI.

For example, if you would like to provide a NumberFormat object for a new locale, you have to extend the java.text.spi.NumberFormatProvider class and implement the following methods:

  • getCurrencyInstance(Locale locale)
  • getIntegerInstance(Locale locale)
  • getNumberInstance(Locale locale)
  • getPercentInstance(Locale locale)

You can write your own implementations for most locale-sensitive classes in the java.text and java.util packages for locales that are not (yet) supported by the JRE. Here's a class that creates a date format for a fictional locale called "New Atlantis":

You can now create the date formatter for your new locale by passing it to the DateFormat object's getDateTimeFormat() method. To display a date in your locale's format, you would then call your formatter's format() method:

In the next installment, I'll cover how to handle text messages, including the storage of translatable text in Resource Bundles.


Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .


Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: May 24, 2010


[prev]