Isolating Locale-specific Objects in a ResourceBundle |
This section illustrates the use of aListResourceBundle
object with an example program calledListDemo
. We'll explain each step involved in creating theListDemo
program, along with theListResourceBundle
subclasses that support it. The source code for the program is in ListDemo.java. You may also find it helpful to examine the output produced byListDemo
.1. Create the ListResourceBundle Subclasses
AListResourceBundle
is backed up by a class file. Therefore, our first step is to create a class file for every supportedLocale
. In theListDemo
program, the base name of theListResourceBundle
isStatsBundle
. SinceListDemo
supports three differentLocale
objects, it requires the following class files:The StatsBundle class for Japan is defined in the source code that follows. Note that the class name is constructed by appending the language and country codes to the base name of theStatsBundle_en_CA.class StatsBundle_fr_FR.class StatsBundle_ja_JA.classListResourceBundle
. Inside the class, the two-dimensionalcontents
array is initialized with the key-value pairs. The keys are the first element in each pair: GDP, Population, and Literacy. The keys must beString
objects, and they must be the same in every class in theStatsBundle
set. The values can be any type of object. In this example, the values are twoInteger
objects and aFloat
object.import java.util.*; public class StatsBundle_ja_JA extends ListResourceBundle { public Object[][] getContents() { return contents; } private Object[][] contents = { {"GDP", new Integer(21300)}, {"Population", new Integer(125449703)}, {"Literacy", new Double(0.99)}, }; }2. Specify the Locale
In theListDemo
program, we define theLocale
objects as follows:EachLocale[] supportedLocales = { new Locale("en","CA"), new Locale("ja","JA"), new Locale("fr","FR") };Locale
object corresponds to one of theStatsBundle
classes. For example, the JapaneseLocale
, which was defined with theja
andJA
codes, matches theStatsBundle_ja_JA.class
.3. Create the ResourceBundle
To create theListResourceBundle
, we invoke thegetBundle
method. In the following line of code, note that we specify the base name of the class (StatsBundle
) and theLocale
.TheResourceBundle stats = ResourceBundle.getBundle("StatsBundle",currentLocale);getBundle
method will searches for a class whose name begins withStatsBundle
and is followed by the language and country codes of the specifiedLocale
. For example, if thecurrentLocale
is created with theja
andJA
codes, thengetBundle
returns aListResourceBundle
loaded from classStatsBundle_ja_JA
.4. Fetch the Localized Objects
Now that we have aListResourceBundle
for the appropriateLocale
, we can fetch the localized objects by their keys. In the following line of code, we retrieve the literacy rate by invokinggetObject
with the "Literacy" key parameter. SincegetObject
returns an object, we must cast it to aDouble
:Double lit = (Double)stats.getObject("Literacy");
Isolating Locale-specific Objects in a ResourceBundle |