Fine Tuning Your Java 6 Database-Driven ResourceBundle | 2 | WebReference

Fine Tuning Your Java 6 Database-Driven ResourceBundle | 2


Fine Tuning Your Java 6 Database-Driven ResourceBundle [con't]

Here is the output (with a little extra formatting for better readability):

Specifying the Order of Bundle Lookups

Each time the ResourceBundle.getBundle() factory method tries to find a resource bundle for a target locale, it calls the ResourceBundle.Control class' getCandidateList() method to come up with the parent chain. Should you wish to load your bundles in a different order than the following, you'll have to override getCandidateList():

  • Locale(language, country, variant)
  • Locale(language, country)
  • Locale(language)
  • Locale.ROOT

Adding the following overridden getCandidateLocales() to the ResourceBundle.Control class sets English as the default language, no matter what the country. Otherwise, if a bundle for the requested language is not found, then that of the default locale is used. In a Web application, that would be the server's locale:

Another useful method for specifying the order of bundle lookups is getFallbackLocale(). It returns a locale to be used as a fallback locale for further ResourceBundle searches by the ResourceBundle.getBundle() factory method. This method is called from the factory method every time when no resulting resource bundle has been found for baseName and locale, where locale is either the parameter for ResourceBundle.getBundle() or the previous fallback locale returned by this method. You can override this method to return a locale other than the default one. The following code would substitute the US locale for my default of English Canada (en_CA):

That brings us to the end of our exploration of Java 6's ResourceBundle classes. I think that you'll agree that they are easily adaptable and scalable to whatever format your display text is stored in. From .property files to commercial relational databases, ResourceBundles can be tuned to work with them all.

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 release is called "The Rabbit of Seville". Loosely based on Rossini's The Barber of Seville overture, Rob's amazing rendition includes a full orchestra and numerous guitar tracks. It is sold on his site as a high bitrate MP3 for only $0.99 cents! 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: July 19, 2010