Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Conceptually eachResourceBundle
is a set of related subclasses that share the same base name. The list that follows shows a set of related subclasses.ButtonLabel
is the base name. The characters following the base name indicate the language code, country code, and variant of aLocale
.ButtonLabel_en_GB
, for example, matches theLocale
specified by the language code for English (en
) and the country code for Great Britain (GB
).ButtonLabel ButtonLabel_de ButtonLabel_en_GB ButtonLabel_fr_CA_UNIXTo select the appropriate
ResourceBundle
, invoke theResourceBundle.getBundle
method. The following example selects theButtonLabel
ResourceBundle
for theLocale
that matches the French language, the country of Canada, and the UNIX platform.Locale currentLocale = new Locale("fr", "CA", "UNIX"); ResourceBundle introLabels = ResourceBundle.getBundle("ButtonLabel", currentLocale);If a
ResourceBundle
class for the specifiedLocale
does not exist,getBundle
tries to find the closest match. For example, ifButtonLabel_fr_CA_UNIX
is the desired class and the defaultLocale
isen_US
,getBundle
will look for classes in the following order:ButtonLabel_fr_CA_UNIX ButtonLabel_fr_CA ButtonLabel_fr ButtonLabel_en_US ButtonLabel_en ButtonLabelNote that
getBundle
looks for classes based on the defaultLocale
before it selects the base class (ButtonLabel)
. IfgetBundle
fails to find a match in the preceding list of classes, it throws aMissingResourceException
. To avoid throwing this exception, you should always provide a base class with no suffixes.
The abstract classResourceBundle
has two subclasses:PropertyResourceBundle
andListResourceBundle
.A
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 forString
objects only. If you need to store other types of objects, use aListResourceBundle
instead. The section Backing a ResourceBundle with Properties Files shows you how to use aPropertyResourceBundle
.The
ListResourceBundle
class manages resources with a convenient list. EachListResourceBundle
is backed by a class file. You can store any locale-specific object in aListResourceBundle
. To add support for an additionalLocale
, you create another source file and compile it into a class file. The section Using a ListResource Bundle has a coding example you may find helpful.The
ResourceBundle
class is flexible. If you first put your locale-specificString
objects in aPropertyResourceBundle
and then later decided to useListResourceBundle
instead, there is no impact on your code. For example, the following call togetBundle
will retrieve aResourceBundle
for the appropriateLocale
, whetherButtonLabel
is backed up by a class or by a properties file:ResourceBundle introLabels = ResourceBundle.getBundle("ButtonLabel", currentLocale);
ResourceBundle
objects contain an array of key-value pairs. You specify the key, which must be aString
, when you want to retrieve the value from theResourceBundle
. The value is the locale-specific object. The keys in the following example are theOkKey
andCancelKey
strings:class ButtonLabel_en extends ListResourceBundle { // English version public Object[][] getContents() { return contents; } static final Object[][] contents = { {"OkKey", "OK"}, {"CancelKey", "Cancel"}, }; }To retrieve the
OK
String
from theResourceBundle
, you would specify the appropriate key when invokinggetString
:String okLabel = ButtonLabel.getString("OkKey");A properties file contains key-value pairs. The key is on the left side of the equal sign, and the value is on the right. Each pair is on a separate line. The values may represent
String
objects only. The following example shows the contents of a properties file namedButtonLabel.properties
:OkKey = OK CancelKey = Cancel
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.