|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section illustrates the use of aListResourceBundleobject with a sample program calledListDemo. The text that follows explains each step involved in creating the
ListDemoprogram, along with theListResourceBundlesubclasses that support it.
AListResourceBundleis backed up by a class file. Therefore the first step is to create a class file for every supportedLocale. In theListDemoprogram the base name of theListResourceBundleisStatsBundle. SinceListDemosupports threeLocaleobjects, it requires the following three class files:StatsBundle_en_CA.class StatsBundle_fr_FR.class StatsBundle_ja_JP.classThe
StatsBundleclass 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-dimensionalcontentsarray is initialized with the key-value pairs. The keys are the first element in each pair:GDP,Population, andLiteracy. The keys must beStringobjects and they must be the same in every class in theStatsBundleset. The values can be any type of object. In this example the values are twoIntegerobjects and aDoubleobject.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) }, }; }
TheListDemoprogram defines theLocaleobjects as follows:EachLocale[] supportedLocales = { new Locale("en", "CA"), new Locale("ja", "JP"), new Locale("fr", "FR") };Localeobject corresponds to one of theStatsBundleclasses. For example, the JapaneseLocale, which was defined with thejaandJPcodes, matchesStatsBundle_ja_JP.class.
To create theListResourceBundle, invoke thegetBundlemethod. The following line of code specifies the base name of the class (StatsBundle) and theLocale:ResourceBundle stats = ResourceBundle.getBundle("StatsBundle", currentLocale);The
getBundlemethod searches for a class whose name begins withStatsBundleand is followed by the language and country codes of the specifiedLocale. If thecurrentLocaleis created with thejaandJPcodes,getBundlereturns aListResourceBundlecorresponding to the classStatsBundle_ja_JP, for example.
Now that the program has aListResourceBundlefor the appropriateLocale, it can fetch the localized objects by their keys. The following line of code retrieves the literacy rate by invokinggetObjectwith theLiteracykey parameter. SincegetObjectreturns an object, cast it to aDouble:Double lit = (Double)stats.getObject("Literacy");
ListDemoprogram prints the data it fetched with thegetBundlemethod: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
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.