A ListResourceBundle is backed up by a class file.
Therefore the first step is to create a class file for every supported
Locale. In the ListDemo program the base name
of the ListResourceBundle is StatsBundle.
Since ListDemo supports three Locale objects,
it requires the following three class files:
StatsBundle_en_CA.class
StatsBundle_fr_FR.class
StatsBundle_ja_JP.class
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 the
ListResourceBundle. Inside the class the two-dimensional
contents array is initialized with the key-value pairs.
The keys are the first element in each pair: GDP,
Population, and Literacy. The keys must be
String objects and they must be the same in every class in
the StatsBundle set. The values can be any type of object.
In this example the values are two Integer objects and a
Double 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) },
};
}
To create the ListResourceBundle, invoke the
getBundle method. The following line of code specifies
the base name of the class (StatsBundle) and the
Locale:
ResourceBundle stats =
ResourceBundle.getBundle("StatsBundle", currentLocale);
The getBundle method searches for a class whose name
begins with StatsBundle and is followed by the language
and country codes of the specified Locale. If the
currentLocale is created with the ja and
JP codes, getBundle returns a
ListResourceBundle corresponding to the class
StatsBundle_ja_JP, for example.