Isolating Locale-Specific Data |
This section illustrates the use of aListResourceBundle
object with a sample program calledListDemo
. The text that follows explains each step involved in creating theListDemo
program, along with theListResourceBundle
subclasses that support it.1. Create the ListResourceBundle Subclasses
AListResourceBundle
is backed up by a class file. Therefore the first step is to create a class file for every supportedLocale
. In theListDemo
program the base name of theListResourceBundle
isStatsBundle
. SinceListDemo
supports threeLocale
objects, it requires the following three class files:StatsBundle_en_CA.class StatsBundle_fr_FR.class StatsBundle_ja_JP.classThe
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 theListResourceBundle
. 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
, andLiteracy
. 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 aDouble
object.import java.util.*; public class StatsBundle_ja_JP 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
TheListDemo
program defines theLocale
objects as follows:EachLocale[] supportedLocales = { new Locale("en", "CA"), new Locale("ja", "JP"), new Locale("fr", "FR") };Locale
object corresponds to one of theStatsBundle
classes. For example, the JapaneseLocale
, which was defined with theja
andJP
codes, matchesStatsBundle_ja_JP.class
.3. Create the ResourceBundle
To create theListResourceBundle
, invoke thegetBundle
method. The following line of code specifies the base name of the class (StatsBundle
) and theLocale
:ResourceBundle stats = ResourceBundle.getBundle("StatsBundle", currentLocale);The
getBundle
method searches for a class whose name begins withStatsBundle
and is followed by the language and country codes of the specifiedLocale
. If thecurrentLocale
is created with theja
andJP
codes,getBundle
returns aListResourceBundle
corresponding to the classStatsBundle_ja_JP
, for example.4. Fetch the Localized Objects
Now that the program has aListResourceBundle
for the appropriateLocale
, it can fetch the localized objects by their keys. The following line of code retrieves the literacy rate by invokinggetObject
with theLiteracy
key parameter. SincegetObject
returns an object, cast it to aDouble
:Double lit = (Double)stats.getObject("Literacy");5. Run the Demo Program
ListDemo
program prints the data it fetched with thegetBundle
method:Locale = en_CA GDP = 24400 Population = 28802671 Literacy = 0.97 Locale = ja_JP GDP = 21300 Population = 125449703 Literacy = 0.99 Locale = fr_FR GDP = 20200 Population = 58317450 Literacy = 0.99
Isolating Locale-Specific Data |