ResourceBundle is a set of related subclasses that share the same base
name. The list that follows shows a set of related subclasses.
MyApp is the base
name. The characters following the base name indicate the language code, country code, and variant of
MyApp_en_GB, for example, matches the
Locale specified by
the language code for English (
en) and the country code for Great Britain (
MyApp.class MyApp_de.class MyApp_en_GB.class MyApp_fr_CA_UNIX.class
To select the appropriate
ResourceBundle, invoke the
ResourceBundle.getBundle(...) method. The
following example selects the
ResourceBundle for the
Locale that matches the
French language, the country of Canada, and the UNIX platform.
Locale locale = new Locale("fr", "CA", "UNIX"); ResourceBundle introLabels = ResourceBundle.getBundle("MyApp", locale);
getBundle(...) looks for classes based on the default
Locale before it selects the base class (
getBundle(...) fails to find a match in the preceding list of classes, it throws a
MissingResourceException. To avoid
throwing this exception, you should always provide a base class with no suffixes.
The abstract class
ResourceBundle has two subclasses:
PropertyResourceBundle is backed by a properties file. A properties file is a plain-text file that contains
translatable text. Properties files are not part of the Java source code, and they can contain values for String objects only.
If you need to store other types of objects, use a
A property resource bundle is a text file of "
key=value" pairs such as:
These are stored in a file
NOTE: the default property resource bundle (for situations when there is no match for
locale) will have
The English USA version would be in file
The Belarusian version would be in file
ListResourceBundle class manages resources with a convenient list. Each
ListResourceBundle is backed
by a class file. You can store any locale-specific object in a
ListResourceBundle. To add support for an additional
Locale, you create another source file and compile it into a class file.