Writing Global Programs |
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: " }, { "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: " }, { "GDPLabel", "Per Capita GDP_FR: " }, { "PopulationLabel", "Population_FR: " }, { "LiteracyLabel", "Literacy Rate_FR: " }, // END "LOCALIZE THIS" }; }Note that this bundle contains French language translations for the data contained in LabelsBundle and is appropriate for any French-speaking country. Thus this bundle is used by both the Locale.FRANCE LinguaPanel and the Locale.CANADA_FRENCH LinguaPanel.
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.FRANCE or Locale.CANADA_FRENCH 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,
getResourceBundle
, 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.getResourceBundle("ParagraphBundle", locale, loader);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
.France and French Canada cannot share ParagraphBundles because the paragraph provides a description of the locale. The language that the paragraphs are written in is the same for each locale, however, the contents are different. Thus AroundTheWorld must provide a two different properties files:
ParagraphBundle_fr.properties
andParagraphBundle_fr_CA.properties
.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_fr_CA.properties
.To retrieve text from a PropertyResourceBundle you useLocaleDescription=Canada is the world's second largest country (after Russia) and consists of ten provinces and two territories. Canada is located in North America north of the US. French Canada is comprised of one province, Quebec, whose legal system is based on French law.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.
Writing Global Programs |