Previous | Next | Trail Map | Internationalization | Isolating Locale-specific Objects in a ResourceBundle

Using a ListResourceBundle

This section illustrates the use of a ListResourceBundle object with an example program called ListDemo. We'll explain each step involved in creating the ListDemo program, along with the ListResourceBundle 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 by ListDemo.

1. Create the ListResourceBundle Subclasses

A ListResourceBundle is backed up by a class file. Therefore, our 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 different Locale objects, it requires the following class files:
StatsBundle_en_CA.class
StatsBundle_fr_FR.class
StatsBundle_ja_JA.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 Float 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 the ListDemo program, we define the Locale objects as follows:
Locale[] supportedLocales = {
   new Locale("en","CA"),
   new Locale("ja","JA"),
   new Locale("fr","FR")
};
Each Locale object corresponds to one of the StatsBundle classes. For example, the Japanese Locale, which was defined with the ja and JA codes, matches the StatsBundle_ja_JA.class.

3. Create the ResourceBundle

To create the ListResourceBundle, we invoke the getBundle method. In the following line of code, note that we specify the base name of the class (StatsBundle) and the Locale.
ResourceBundle stats = 
   ResourceBundle.getBundle("StatsBundle",currentLocale);
The getBundle method will searches for a class whose name begins with StatsBundle and is followed by the language and country codes of the specified Locale. For example, if the currentLocale is created with the ja and JA codes, then getBundle returns a ListResourceBundle loaded from class StatsBundle_ja_JA.

4. Fetch the Localized Objects

Now that we have a ListResourceBundle for the appropriate Locale, we can fetch the localized objects by their keys. In the following line of code, we retrieve the literacy rate by invoking getObject with the "Literacy" key parameter. Since getObject returns an object, we must cast it to a Double:
Double lit = (Double)stats.getObject("Literacy");


Previous | Next | Trail Map | Internationalization | Isolating Locale-specific Objects in a ResourceBundle