Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data


Storing and Accessing Strings in Resource Bundles

For managing its strings AroundTheWorld uses two different resource bundles:
  1. its own subclass of ListResourceBundle, LabelsBundle, to manage the labels for its GUI elements
  2. 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:

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"
    };
}
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.

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:

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"));
When the Locale.US LinguaPanel invokes these statements, 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:

ResourceBundle paragraph = ResourceBundle.getBundle("ParagraphBundle", locale);
However, unlike other resource bundles, the data for PropertyResourceBundles are contained in a properties file. Here's ParagraphBundle.properties (the default properties file for ParagraphBundle):
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.
The key appears on the left-hand side of the equals (=) 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 no ParagraphBundle_en_US.properties or ParagraphBundle_en.properties.

AroundTheWorld has two additional properties files: ParagraphBundle_fr.properties and ParagraphBundle_en_GB.properties for the other two supported locales.

Here's ParagraphBundle_fr.properties.

LocaleDescription=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.
And here's ParagraphBundle_en_GB.properties.
LocaleDescription=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.
To retrieve text from a PropertyResourceBundle you use getString with the key for the property that you want to get:
description = new TextArea(paragraph.getString("LocaleDescription"),
                7, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
When the Locale.US LinguaPanel invokes these statements, paragraphs is a PropertyResourceBundle object whose values were loaded from ParagraphBundle.properties. And so on for the other two LinguaPanels.


Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data