Gets a resource bundle using the specified base name, locale, and class loader.
Conceptually, getBundle
uses the following strategy for locating and instantiating
resource bundles:
getBundle
uses the base name, the specified locale, and the default
locale (obtained from Locale.getDefault
)
to generate a sequence of candidate bundle names.
If the specified locale's language, country, and variant are all empty
strings, then the base name is the only candidate bundle name.
Otherwise, the following sequence is generated from the attribute
values of the specified locale (language1, country1, and variant1)
and of the default locale (language2, country2, and variant2):
- baseName + "_" + language1 + "_" + country1 + "_" + variant1
- baseName + "_" + language1 + "_" + country1
- baseName + "_" + language1
- baseName + "_" + language2 + "_" + country2 + "_" + variant2
- baseName + "_" + language2 + "_" + country2
- baseName + "_" + language2
- baseName
Candidate bundle names where the final component is an empty string are omitted.
For example, if country1 is an empty string, the second candidate bundle name is omitted.
getBundle
then iterates over the candidate bundle names to find the first
one for which it can instantiate an actual resource bundle. For each candidate
bundle name, it attempts to create a resource bundle:
-
First, it attempts to load a class using the candidate bundle name.
If such a class can be found and loaded using the specified class loader, is assignment
compatible with ResourceBundle, is accessible from ResourceBundle, and can be instantiated,
getBundle
creates a new instance of this class and uses it as the result
resource bundle.
-
Otherwise,
getBundle
attempts to locate a property resource file.
It generates a path name from the candidate bundle name by replacing all "." characters
with "/" and appending the string ".properties".
It attempts to find a "resource" with this name using
ClassLoader.getResource
.
(Note that a "resource" in the sense of getResource
has nothing to do with
the contents of a resource bundle, it is just a container of data, such as a file.)
If it finds a "resource", it attempts to create a new
PropertyResourceBundle
instance from its contents.
If successful, this instance becomes the result resource bundle.
If no result resource bundle has been found, a MissingResourceException
is thrown.
Once a result resource bundle has been found, its parent chain is instantiated.
getBundle
iterates over the candidate bundle names that can be
obtained by successively removing variant, country, and language
(each time with the preceding "_") from the bundle name of the result resource bundle.
As above, candidate bundle names where the final component is an empty string are omitted.
With each of the candidate bundle names it attempts to instantiate a resource bundle, as
described above.
Whenever it succeeds, it calls the previously instantiated resource
bundle's setParent
method
with the new resource bundle, unless the previously instantiated resource
bundle already has a non-null parent.
getBundle
caches instantiated resource bundles and
may return the same resource bundle instance multiple
times.
The baseName
argument should be a fully qualified class name. However, for
compatibility with earlier versions, Sun's Java SE Runtime Environments do not verify this,
and so it is possible to access PropertyResourceBundle
s by specifying a
path name (using "/") instead of a fully qualified class name (using ".").
Example:
The following class and property files are provided:
MyResources.class
MyResources.properties
MyResources_fr.properties
MyResources_fr_CH.class
MyResources_fr_CH.properties
MyResources_en.properties
MyResources_es_ES.class
The contents of all files are valid (that is, public non-abstract subclasses of
ResourceBundle
for
the ".class" files, syntactically correct ".properties" files).
The default locale is
Locale("en", "GB")
.
Calling getBundle
with the shown locale argument values instantiates
resource bundles from the following sources:
- Locale("fr", "CH"): result MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class
- Locale("fr", "FR"): result MyResources_fr.properties, parent MyResources.class
- Locale("de", "DE"): result MyResources_en.properties, parent MyResources.class
- Locale("en", "US"): result MyResources_en.properties, parent MyResources.class
- Locale("es", "ES"): result MyResources_es_ES.class, parent MyResources.class
The file MyResources_fr_CH.properties is never used because it is hidden by
MyResources_fr_CH.class. Likewise, MyResources.properties is also hidden by
MyResources.class.
Returns:
a resource bundle for the given base name and locale
Parameters:
- baseName - the base name of the resource bundle, a fully qualified class name
- locale - the locale for which a resource bundle is desired
- loader - the class loader from which to load the resource bundle
Throws:
- java.lang.NullPointerException - if baseName
, locale
, or loader
is null
- MissingResourceException - if no resource bundle for the specified base name can be found
Since:
1.2