Locale-Sensitive Data |
For managing its strings AroundTheWorld uses two different resource bundles:
- its own subclass of ListResourceBundle, LabelsBundle, to manage the labels for its GUI elements
- a PropertyResourceBundle to manage the paragraph description of the locale
List Resource Bundles
Let's begin with the LabelsBundle class:import java.util.ListResourceBundle; public class LabelsBundle extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { // LOCALIZE THIS { "LocaleLabel", "Locale: " }, { "DateLabel", "Today's Date: " }, { "TimeLabel", "Current Time (in {0}): " }, { "RepCity", "San Francisco" }, { "TimeZone", "PST" }, { "GDPLabel", "Per Capita GDP: " }, { "PopulationLabel", "Population: " }, { "LiteracyLabel", "Literacy Rate: " }, // END "LOCALIZE THIS" }; }LabelsBundle is a subclass of ListResourceBundle. As such, it must override the
getContents
method which returns an array of two-element arrays. Each sub-array of this array contains two elements: A key and a value.The
contents
array created by LabelsBundle is built statically and contains only Strings. The values are the strings AroundTheWorld uses to display the text labels in its LinguaPanels:
The AroundTheWorld program defines one other version of LabelsBundle, the French language version, of LabelsBundle--LabelsBundle_fr:
LabelsBundle_fr is the same as LabelsBundle, except that the values have been translated into French. Note that the keys have not changed. The keys are used to look up values in the bundle. So, you must use the same keys for the same items in related ResourceBundles.import java.util.ListResourceBundle; public class LabelsBundle_fr extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { // LOCALIZE THIS { "LocaleLabel", "Locale_FR: " }, { "DateLabel", "Date_FR: " }, { "TimeLabel", "Current Time (in {0})_FR:" }, { "RepCity", "Paris" }, { "TimeZone", "ECT" }, { "GDPLabel", "Per Capita GDP_FR: " }, { "PopulationLabel", "Population_FR: " }, { "LiteracyLabel", "Literacy Rate_FR: " }, // END "LOCALIZE THIS" }; }
To retrieve a specific string value from a ResourceBundle use
getString
. Here's the code that LinguaPanel uses to extract the labels for the textfields from LabelsBundle:When the Locale.US LinguaPanel invokes these statements,localeLabel.setText(labels.getString("LocaleLabel")); . . . dateLabel.setText(labels.getString("DateLabel")); . . . gdpLabel.setText(labels.getString("GDPLabel")); . . . populationLabel.setText(labels.getString("PopulationLabel")); . . . literacyLabel.setText(labels.getString("LiteracyLabel"));labels
is a LabelsBundle object. When the Locale.UK LinguaPanel invokes these statements,labels
is a LabelsBundle_en_GB object. And when the Locale.FRANCE LinguaPanel invokes these statements,labels
is a LabelsBundle_fr object. Thus the GUI elements reflect the current locale.Property Resource Bundles
AroundTheWorld uses a PropertyResourceBundle named ParagraphBundle to manage the text information that it displays in the TextArea of the window. This information is simply a paragraph describing the locale.
As you saw in Loading Resource Bundles, you use the same method,
getBundle
, to load a property bundle as you do to load a list resource bundle. Here's the statement LinguaPanel uses to load ParagraphBundle:However, unlike other resource bundles, the data for PropertyResourceBundles are contained in a properties file. Here'sResourceBundle paragraph = ResourceBundle.getBundle("ParagraphBundle", locale);ParagraphBundle.properties
(the default properties file for ParagraphBundle):The key appears on the left-hand side of the equals (LocaleDescription=The United States consists of fifty states plus the District of Columbia. The US is the world's fourth largest country (after Russia, Canada, and China) and is located in North America between Canada and Mexico.=
) sign. Its corresponding value appears on the right-hand side. As with LabelsBundle, this is the version that is loaded for Locale.US (because there is noParagraphBundle_en_US.properties
orParagraphBundle_en.properties
.AroundTheWorld has two additional properties files:
ParagraphBundle_fr.properties
andParagraphBundle_en_GB.properties
for the other two supported locales.Here's
ParagraphBundle_fr.properties
.And here'sLocaleDescription=France is the largest Western European country and is a member of the EEC. France shares borders with Andorra, Belgium, Germany, Italy, Luxembourg, Manaco, Spain and Switzerland.ParagraphBundle_en_GB.properties
.To retrieve text from a PropertyResourceBundle you useLocaleDescription=The United Kingdom is an island nation located in Western Europe, bordering on the North Atlantic Ocean and the North Sea, between Ireland and France. The UK is slightly smaller than the state of Oregon in the United States and is a member of the EEC.getString
with the key for the property that you want to get:When the Locale.US LinguaPanel invokes these statements,description = new TextArea(paragraph.getString("LocaleDescription"), 7, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);paragraphs
is a PropertyResourceBundle object whose values were loaded fromParagraphBundle.properties
. And so on for the other two LinguaPanels.
Locale-Sensitive Data |