Java Reference
In-Depth Information
If getBundle() cannot find a class or properties file for the specified locale in any
of the four previous search steps, it repeats the search using the default locale
instead of the specified locale. If no appropriate ResourceBundle is found in this
search either, getBundle() throws a MissingResourceException .
Any ResourceBundle object can have a parent ResourceBundle specified for it.
When you look up a named resource in a ResourceBundle , getObject() first looks
in the specified bundle, but if the named resource is not defined in that bundle, it
recursively looks in the parent bundle. Thus, every ResourceBundle inherits the
resources of its parent and may choose to override some, or all, of these
resources. (Note that we are using the terms “inherit” and “override” in a different
sense than we do when talking about classes that inherit and override methods in
their superclass.) What this means is that every ResourceBundle you define does
not have to define every resource required by your application. For example, you
might define a ResourceBundle of messages to display to French-speaking users.
Then you might define a smaller and more specialized ResourceBundle that over-
rides a few of these messages so that they are appropriate for French-speaking
users who live in Canada.
Your application is not required to find and set up the parent objects for the
ResourceBundle objects it uses. The getBundle() method actually does this for
you. When getBundle() finds an appropriate class or properties file as described
above, it does not immediately return the ResourceBundle it has found. Instead, it
continues through the remaining steps in the previous search process, looking for
less-specific class or properties files from which the ResourceBundle may inherit
resources. If and when getBundle() finds these less-specific resource bundles, it
sets them up as the appropriate ancestors of the original bundle. Only once it has
checked all possibilities does it return the original ResourceBundle object that it
created.
To continue the example begun earlier, when a program runs in Quebec, getBun-
dle() might first find a small specialized ResourceBundle class that has only a few
specific Quebecois resources. Next, it looks for a more general ResourceBundle
that contains French messages, and it sets this bundle as the parent of the original
Quebecois bundle. Finally, getBundle() looks for (and probably finds) a class the
defines a default set of resources, probably written in English (assuming that
English is the native tongue of the original programmer). This default bundle is set
as the parent of the French bundle (which makes it the grandparent of the Quebe-
cois bundle). When the application looks up a named resource, the Quebecois
bundle is searched first. If the resource isn't defined there, the French bundle is
searched, and any named resource not found in the French bundle is looked up in
the default bundle.
ResourceBundle Example
Examining some code makes this discussion of resource bundles much clearer.
Example 7-4 is a convenience routine for creating Swing menu panes. Given a list
of menu item names, it looks up labels and menu shortcuts for those named menu
items in a resource bundle and creates a localized menu pane. The example has a
simple test program attached.
Search WWH ::




Custom Search