![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
This section illustrates the use of aListResourceBundle
object with a sample program called
ListDemo
. The text that follows explains each step involved in creating the
ListDemo
program, along with theListResourceBundle
subclasses that support it.
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) }, }; }
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
.
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.
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");
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
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |