|
|
Isolating Locale-Specific Data |
How a ResourceBundle is Related to a Locale
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. For instance,ButtonLabel_en_GBmatches theLocalespecified by the language code for English (en) and the country code for the U.K. (GB).ButtonLabel ButtonLabel_de ButtonLabel_en_GB ButtonLabel_fr_CA_UNIXTo select the appropriate
ResourceBundle, you 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, if a class forButtonLabel_fr_CA_UNIXdoes not exist,getBundlewill look for classes in the following order:IfButtonLabel_fr_CA_UNIX ButtonLabel_fr_CA ButtonLabel_fr ButtonLabelgetBundlefails to find a match in the preceding list of classes, then it will attempt a similar search using the defaultLocale. Failing there,getBundlewill throw aMissingResourceException.You should always provide a base class with no suffixes. In the previous example, if a class named
ButtonLabelexists, thengetBundlewill not throw aMissingResourceException.The ListResourceBundle and PropertyResourceBundle Subclasses
The abstract classResourceBundlehas two subclasses:ListResourceBundleandPropertyResourceBundle. The subclass you choose depends on the type of data, and how it is to be localized.A
PropertyResourceBundleis backed by one or more properties files. You should store translatableStringobjects in properties files. Since the properties files are simple text files and are not part of the Java source code, they can be created and updated by translators. No programming expertise is required. A translator can add support for an additionalLocalemerely by creating a new properties file. A new class file is not needed. Properties files can contain values forStringobjects only. If you need to store other types of objects, use aListResourceBundleinstead. 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 must create another source file and compile it into a class file. Since translators are not usually programmers, you should not storeStringobjects that require translation in aListResourceBundle. Using a ListResourceBundle contains a coding example you may find helpful.The
ResourceBundleclass is flexible. If you first loaded your locale-specificStringobjects in aListResourceBundle, and then later decided to usePropertyResourceBundleinstead, the impact on your code will be limited. 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);Key-Value Pairs
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. In the following example, the keys are the "OkKey" and "CancelKey"Stringobjects:To retrieve the "OK"class ButtonLabel_en extends ListResourceBundle { // English version public Object[][] getContents() { return contents; } static final Object[][] contents = { {"OkKey", "OK"}, {"CancelKey", "Cancel"}, }; }Stringfrom theResourceBundle, you would specify the appropriate key when invokinggetString:The preceding example is simplistic, because theString okLabel = ButtonLabel.getString("OkKey");Stringvalues are hardcoded in the source code. This is not good practice, because your translators need to work with properties files which are separate from the source code.A properties file contains key-value pairs. The key is on the left side of the equals 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
|
|
Isolating Locale-Specific Data |