Conceptually, each ResourceBundle is a set of related subclasses that share the same base name.
The list that follows shows a set of related subclasses.
The characters following the base name indicate the language code, country code, and variant of a Locale.
BaseName_language_country_variant
For instance, MyProgramResource _en_GB matches the Locale specified by the language code for English (en) and the country code for the U.K. (GB).
To select the appropriate ResourceBundle, you invoke the getBundle method.
The following example selects the MyProgramResource ResourceBundle for the Locale that matches the Locale referenced by currentLocale.
Actually, getBundle is overloaded.
So, you can simply pass it a bundle name and expect it to return the bundle for the default locale, and also pass a specific locale.
This method provides a graceful degradation algorithm that attempts to find the nearest matched bundle and attempts to load the class that matches the locale specified by language, country and variant.
If not successful, first the variant then the country and finally the language is dropped.
That is, the getBundle method tries to load one of the following until successful in this order:
If all these attempts fail then a MissingResourceException is thrown.
Once the getBundle method locates a particular class it continues to look for the remaining classes and if found they become the parents in the resource hierarchy for the located class.
Parents come in handy if there is a failure when looking up an object within the located class.
In such a case the parents are queried for that object.
This relationship is identified among bundles by giving them the same base name.
You should always provide a base class with no suffixes which will act as the parent and in case the resource is queried for a particular locale that doesn't exist, the default values will still be found.
Now that we know how to name and locate a resource lets get down to the nity gritty of placing the locale dependant information into these resource bundles.
Sameer Tyagi is a Software Engineer with several years of programming experience in iNet application development and has conducted multiple training workshops in Java. Besides holding an Engineering degree in Electronics he is a Sun Certified Java 1.1 Programmer.
Email: sameertyagi@usa.net
JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for
Windows 95, 98, ME, NT, 2000 and XP. It's designed to
automatically optimize your Windows settings, boosting your
Internet connection up to 200%.
Blackmail Applet
This applet prints out text that looks like letters cut out of a
newspaper. You can specify the text, the width and height of the
applet and the delay after each letter. Future versions will
have more parameters.
While surveys suggest that small businesses aren't prepared for ever-present security dangers, this Boston-area printer has maintained a pristine operation since a scare six years ago.