Java Reference
In-Depth Information
subclass
ListResourceBundle
. You can also simply provide a property file (see the
java.util.Properties
class) that
ResourceBundle.getBundle()
uses to create an
instance of
PropertyResourceBundle
.
To use localized resources from a
ResourceBundle
in a program, you should first
call the static
getBundle()
method, which dynamically loads and instantiates a
ResourceBundle
, as described shortly. The returned
ResourceBundle
has the name
you specify and is appropriate for the specified locale (or for the default locale, if
no locale is explicitly specified). Once you have obtained a
ResourceBundle
object
with
getBundle()
, use the
getObject()
method to look up resources by name.
Note that there is a convenience method,
getString()
, that simply casts the value
returned by
getObject()
to be a
String
object.
When you call
getBundle()
, you specify the base name of the desired
Resource-
Bundle
and a desired locale (if you don't want to rely on the default locale). Recall
that a
Locale
is specified with a two-letter language code, an optional two-letter
country code, and an optional variant string.
getBundle()
looks for an appropriate
ResourceBundle
class for the locale by appending this locale information to the
base name for the bundle. The method looks for an appropriate class with the fol-
lowing algorithm:
1.
Search for a class with the following name:
basename
_
language
_
country
_
variant
If no such class is found, or no variant string is specified for the locale, it goes
to the next step.
2.
Search for a class with the following name:
basename
_
language
_
country
If no such class is found, or no country code is specified for the locale, it goes
to the next step.
3.
Search for a class with the following name:
basename
_
language
If no such class is found, it goes to the final step.
4. Search for a class which has the same name as the basename, or in other
words, search for a class with the following name:
basename
This represents a default resource bundle used by any locale that is not
explicitly supported.
At each step in this process,
getBundle()
checks first for a class file with the given
name. If no class file is found, it uses the
getResourceAsStream()
method of
ClassLoader
to look for a
Properties
file with the same name as the class and a
.pr operties
extension. If such a properties file is found, its contents are used to cre-
ate a
Properties
object and
getBundle()
instantiates and returns a
Property-
ResourceBundle
that exports the properties in the
Properties
file through the
ResourceBundle
API.