THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

private static final Object[][] contents = {
{ GlobalRes.HELLO, "Hello" },
{ GlobalRes.GOODBYE, "Goodbye" },
};
}


This bundle covers the English-language locale en. It provides specific values for each localizable string. The
next bundle uses the inheritance feature:


public class GlobalRes_en_AU extends ListResourceBundle {
// mostly like basic English - our parent bundle


public Object[][] getContents() { return contents; }


private static final Object[][] contents = {
{ GlobalRes.HELLO, "G'day" },
};
}


This bundle is for English speakers from Australia (AU). It provides a more colloquial version of the HELLO
string and inherits all other strings from the general English locale GlobalRes_en. Whenever a resource
bundle is instantiated, its parent chain is established. This proceeds by successively dropping the variant,
country, and language components from the base bundle name and instantiating those bundles if they exist. If
they do exist then setParent is called on the preceding bundle passing the new bundle as the parent. So in
our example, when GlobalRes_en_AU is created, the system will create GlobalRes_en and set it as the
parent of GlobalRes_en_AU. In turn, the parent of GlobalRes_en will be the base bundle
GlobalRes.


Given these classes, someone with an English-language locale (en) would get the values returned by
GlobalRes_en unless the locale also specified the country Australia (AU), in which case values from
GlobalRes_en_AU would be used. Everyone else would see those in GlobalRes.


24.2.2. PropertyResourceBundle


PropertyResourceBundle is a subclass of ResourceBundle that reads its list of resources from a
text property description. Instead of using an array of key/resource pairs, the text contains key/resource pairs
as lines of the form


key=value


Both keys and values must be strings. A PropertyResourceBundle object reads the text from an
InputStream passed to the PropertyResourceBundle constructor and uses the information it reads
to build a lookup table for efficient access.


The bundle search process that we described earlier actually has an additional step that looks for a file
ResName.properties after it looks for a class ResName. For example, if the search process doesn't find
the class GlobalRes_eo_KI_left it will then look for the file
GlobalRes_eo_KI_left.properties before looking for the next resources class. If that file exists, an
input stream is created for it and used to construct a PropertyResourceBundle that will read the

Free download pdf