Writing Global Programs |
Here's the listing ofNumbersBundle
, aListResourceBundle
that contains the data for the GDP, population, and literacy rate.As with the other resource bundles that we've looked at so far,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" }; }NumbersBundle
is the default bundle and contains the data for the United States locale. Notice that where the other bundles had strings,NumbersBundle
createsDouble
andInteger
objects. Any kind of object can be stored in a resource bundle. To retrieve them, you use thegetObject
method provided by theResourceBundle
class. This is the code that LinguaPanel uses to get the GDP, population, and literacy rate fromNumbersBundle
:Note that the object returned fromgdp = (Double) numbers.getObject("GDP"); . . . population = (Integer) numbers.getObject("Population"); . . . literacy = (Double) numbers.getObject("Literacy");getObject
is an Object and may need to be cast to the correct type. In the previous example,gdp
andliteracy
are bothDouble
s andpopulation
is anInteger
. Thus the value returned fromgetObject
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:And here'simport 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" }; }NumbersBundle_fr_CA
which contains the data for French Canada:Using a ListResourceBundle for storing numbers is fairly straightforward. Now, let's look at a more complex example: Using a ListResourceBundle to store images and sounds.import java.util.ListResourceBundle; public class NumbersBundle_fr_CA extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { // LOCALIZE THIS { "GDP", new Double(22200) }, { "Population", new Integer(28113997) }, { "Literacy", new Double(0.97) }, // END "LOCALIZE THIS" }; }
Writing Global Programs |