Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data


Storing and Accessing Other Kinds of Objects in Resource Bundles

Here's the listing of NumbersBundle, a ListResourceBundle that contains the data for the GDP, population, and literacy rate.
import java.util.ListResourceBundle;

public class NumbersBundle extends ListResourceBundle {
    public Object[][] getContents() {
	return contents;
    }
    static final Object[][] contents = {
	// LOCALIZE THIS

        { "GDP",                   new Double(24700) },
        { "Population",            new Integer(260713585) },
        { "Literacy",              new Double(0.97) },

	// END "LOCALIZE THIS"
    };
}
As with the other resource bundles that we've looked at so far, NumbersBundle is the default bundle and contains the data for the United States locale. Notice that where the other bundles had strings, NumbersBundle creates Double and Integer objects. Any kind of object can be stored in a resource bundle. To retrieve them, you use the getObject method provided by the ResourceBundle class. This is the code that LinguaPanel uses to get the GDP, population, and literacy rate from NumbersBundle:
gdp = (Double) numbers.getObject("GDP");
. . .
population = (Integer) numbers.getObject("Population");
. . .
literacy = (Double) numbers.getObject("Literacy");
Note that the object returned from getObject is an Object and may need to be cast to the correct type. In the previous example, gdp and literacy are both Doubles and population is an Integer. Thus the value returned from getObject is appropriately cast in each statement.

The numbers data are displayed next to the text labels in the LinguaPanel. The following is a snapshot of the data for the France locale:

Here's NumbersBundle_fr which contains the data for France:

import java.util.ListResourceBundle;

public class NumbersBundle_fr extends ListResourceBundle {
    public Object[][] getContents() {
	return contents;
    }
    static final Object[][] contents = {
	// LOCALIZE THIS

        { "GDP",                   new Double(18200) },
        { "Population",            new Integer(57840445) },
        { "Literacy",              new Double(0.99) },

	// END "LOCALIZE THIS"
    };
}
And here's NumbersBundle_en_GB which contains the data for the United Kingdom:
import java.util.ListResourceBundle;

public class NumbersBundle_en_GB extends ListResourceBundle {
    public Object[][] getContents() {
	return contents;
    }
    static final Object[][] contents = {
	// LOCALIZE THIS

        { "GDP",                   new Double(16900) },
        { "Population",            new Integer(58135110) },
        { "Literacy",              new Double(0.99) },

	// END "LOCALIZE THIS"
    };
}
Using a ListResourceBundle for storing numbers is fairly straightforward. Now, let's look at a more complex example: Using a ListResourceBundle to store sounds.

[PENDING]


Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data