Conceptually eachResourceBundleis a set of related subclasses that share the same base name. The list that follows shows a set of related subclasses.ButtonLabelis 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 theLocalespecified 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.getBundlemethod. The following example selects theButtonLabelResourceBundlefor theLocalethat 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
ResourceBundleclass for the specifiedLocaledoes not exist,getBundletries to find the closest match. For example, ifButtonLabel_fr_CA_UNIXis the desired class and the defaultLocaleisen_US,getBundlewill look for classes in the following order:ButtonLabel_fr_CA_UNIX ButtonLabel_fr_CA ButtonLabel_fr ButtonLabel_en_US ButtonLabel_en ButtonLabelNote that
getBundlelooks for classes based on the defaultLocalebefore it selects the base class (ButtonLabel). IfgetBundlefails 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 classResourceBundlehas two subclasses:PropertyResourceBundleandListResourceBundle.A
PropertyResourceBundleis 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 forStringobjects only. If you need to store other types of objects, use aListResourceBundleinstead. The section Backing a ResourceBundle with Properties Files shows you how to use aPropertyResourceBundle.The
ListResourceBundleclass manages resources with a convenient list. EachListResourceBundleis 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
ResourceBundleclass is flexible. If you first put your locale-specificStringobjects in aPropertyResourceBundleand then later decided to useListResourceBundleinstead, there is no impact on your code. For example, the following call togetBundlewill retrieve aResourceBundlefor the appropriateLocale, whetherButtonLabelis backed up by a class or by a properties file:ResourceBundle introLabels = ResourceBundle.getBundle("ButtonLabel", currentLocale);
ResourceBundleobjects 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 theOkKeyandCancelKeystrings:class ButtonLabel_en extends ListResourceBundle { // English version public Object[][] getContents() { return contents; } static final Object[][] contents = { {"OkKey", "OK"}, {"CancelKey", "Cancel"}, }; }To retrieve the
OKStringfrom 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
Stringobjects only. The following example shows the contents of a properties file namedButtonLabel.properties:OkKey = OK CancelKey = Cancel