|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section steps through a sample program namedPropertiesDemo.
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 thePropertiesDemoprogram the base name isLabelsBundle. Therefore the default properties file is calledLabelsBundle.properties. This file contains the following lines:Note that in the preceding file the 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,# This is the default LabelsBundle.properties file s1 = computer s2 = disk s3 = monitor s4 = keyboards2is the key that corresponds to the valuedisk. The key is arbitrary. We could have calleds2something else, likemsg5ordiskID. Once defined, however, 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 accommodate additional languages, they will translate the values into various languages.
To support an additionalLocale, 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.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). The contents ofLabelsBundle_de.propertiesare as follows:The# This is the LabelsBundle_de.properties file s1 = Computer s2 = Platte s3 = Monitor s4 = TastaturPropertiesDemosample program ships with three properties files:LabelsBundle.properties LabelsBundle_de.properties LabelsBundle_fr.properties
ThePropertiesDemoprogram creates theLocaleobjects as follows:TheseLocale[] supportedLocales = { Locale.FRENCH, Locale.GERMAN, Locale.ENGLISH };Localeobjects should match the properties files created in the previous two steps. For example, theLocale.FRENCHobject corresponds to theLabelsBundle_fr.propertiesfile. TheLocale.ENGLISHhas no matchingLabelsBundle_en.propertiesfile, so the default file will be used.
This step shows how theLocale, the properties files, and theResourceBundleare related. To create theResourceBundle, invoke thegetBundlemethod, specifying the base name andLocale:ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);The
getBundlemethod first looks for a class file that matches the base name and theLocale. 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 containing the key-value pairs from the properties file.
To retrieve the translated value from theResourceBundle, invoke thegetStringmethod as follows:String value = labels.getString(key);The
Stringreturned bygetStringcorresponds to the key specified. TheStringis in the proper language, provided that a properties file exists for the specifiedLocale.
This step is optional. When debugging your program, you might want to fetch values for all of the keys in aResourceBundle. ThegetKeysmethod returns anEnumerationof all the keys in aResourceBundle. You can iterate through theEnumerationand fetch each value with thegetStringmethod. The following lines of 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); }
Running thePropertiesDemoprogram generates the following output. The first three lines show the values returned bygetStringfor variousLocaleobjects. The program displays the last four lines when iterating through the keys with thegetKeysmethod.Locale = fr, key = s2, value = Disque dur Locale = de, key = s2, value = Platte Locale = en, key = s2, value = disk key = s4, value = Clavier key = s3, value = Moniteur key = s2, value = Disque dur key = s1, value = Ordinateur
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.