|
|
Isolating Locale-Specific Data |
This section steps through a sample program namedPropertiesDemo. The source code for the program is inPropertiesDemo.java.1. Create the Default Properties File
A properties file is a simple text file. You can create and maintain a properties file with just about any text editor.You should always create a default properties file. The name of this file begins with the base name of your
ResourceBundleand ends with the.propertiessuffix. In thePropertiesDemoprogam, the base name is LabelsBundle. Therefore, the default properties file is calledLabelsBundle.properties. This file contains the following lines:In the preceding file, note that comment lines begin with a pound sign (#). The other lines contain key-value pairs. The key is on the left side of the equal sign and the value is on the right. For instance, "s2" is the key that corresponds to the value "disk." The key is arbitrary. We could have called "s2" something else, like "msg5" or "diskID." However, once defined, the key should not change because it is referenced in the source code. The values may be changed. In fact, when your localizers create new properties files to accomodate additional languages, they will translate the values into different languages.# This is the default LabelsBundle.properties file s1 = computer s2 = disk s3 = monitor s4 = keyboard2. Create Additional Properties Files as Needed
To support an addtionalLocale, your localizers will create a new properties file that contains the translated values. No changes to your source code are required, because your program references the keys, not the values.For example, to add support for the German language, your localizers would translate the values in
LabelsBundle.propertiesand place them in a file namedLabelsBundle_de_DE.properties. Notice that the name of this file, like that of the default file, begins with the base nameLabelsBundleand ends with the.propertiessuffix. However, since this file is intended for a specificLocale, the base name is followed by the language code (de) and the country code (DE). The contents ofLabelsBundle_de_DE.propertiesis as follows:The# This is the LabelsBundle_de_DE.properties file s1 = Computer s2 = Platte s3 = Monitor s4 = TastaturPropertiesDemosample program ships with hree properties files:LabelsBundle.properties LabelsBundle_de_DE.properties LabelsBundle_fr.properties3. Specify the Locale
ThePropertiesDemoprogram creates theLocaleobjects as follows:The calls to each of theLocale[] supportedLocales = { new Locale("fr","FR"), new Locale("de","DE"), new Locale("en","US") Locale currentLocale = new Locale("fr","FR");Localeconstructors specify a language code and a country code. These codes match the properties files created in the previous two steps. For example, theLocalecreated with thedeandDEcodes correspond to theLabelsBundle_de_DE.propertiesfile.4. Create the ResourceBundle
This is the step that shows how theLocale, the properties files, and theResourceBundleare related. To create theResourceBundle, invoke thegetBundlemethod, specifying the base name andLocale:TheResourceBundle labels = ResourceBundle.getBundle("LabelsBundle",currentLocale);getBundlemethod first looks for a class file that matches the base name. If it can't find a class file, it then checks for properties files. In thePropertiesDemoprogram, we're backing theResourceBundlewith properties files instead of class files. When thegetBundlemethod locates the correct properties file, it returns aPropertyResourceBundleobject loaded with the key-value pairs from the properties file.If a properties file for the specified
Localedoes not exist,getBundleselects a properties file that is the closest match. The following table identifies the properties file that thePropertiesDemoprogram selects for eachLocale:
Locale Parameters Properties File Loaded Explanation de DELabelsBundle_de_DE.properties Exact match. fr FRLabelsBundle_fr.properties LabelsBundle_fr_FR.properties does not exist
so this is the closest match. en USLabelsBundle.properties The default file is selected because the
Localeparameters do not match.
Instead of invoking
getBundle, you could have created thePropertyResourceBundleobject by invoking its constructor, which accepts anInputStreamas an argument. To create theInputStreamyou must to specify the exact name of the properties file by calling theFileInputStreamconstructor. Creating aPropertyResourceBundleby invokinggetBundleis more flexible, becausegetBundlewill search for the properties file that most closely matches specfiedLocale.5. Fetch the Localized Text
To retrieve the translated value from theResourceBundle, invoke thegetStringmethod:TheString value = labels.getString(key);Stringreturned bygetStringcorresponds to the key specified. TheStringis in the proper language, provided that a properties file exists for the specifiedLocale. Since the keys do not change, the localizers add additional properties files at a later time. The call togetStringneed not change.6. Iterate Through All the Keys
If you want to fetch values for all of the keys in aResourceBundle, you need to invokegetKeys. This method returns anEnumerationof all the keys in aResourceBundle. You can iterate through theEnumerationand fetch each value with thegetStringmethod. The following lines code, which are from thePropertiesDemoprogram, show how this is done:ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle",currentLocale); Enumeration bundleKeys = labels.getKeys(); while (bundleKeys.hasMoreElements()) { String key = (String)bundleKeys.nextElement(); String value = labels.getString(key); System.out.println("key = " + key + ", " + "value = " + value); }7. Run the Demo Program
Running thePropertiesDemoprogram generates the following output. The first group of lines show the values returned bygetStringfor differentLocaleobjects. The program displays the last four lines when iterating through the keys with thegetKeysmethod.Locale = fr_FR, key = s2, value = Disque dur Locale = de_DE, key = s2, value = Platte Locale = en_US, key = s2, value = disk key = s4, value = Clavier key = s3, value = Moniteur key = s2, value = Disque dur key = s1, value = Ordinateur
|
|
Isolating Locale-Specific Data |