THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

properties from the file.


It is easier to use property files than to create subclasses of ListResourceBundle but the files have two
limitations. First, they can only define string resources whereas ListResourceBundle can define
arbitrary objects. Second, the only legal character encoding for property files is the byte format of ISO
8859-1. This means that other Unicode characters must be encoded with \uxxxx escape sequences.


24.2.3. Subclassing ResourceBundle


ListResourceBundle, PropertyResourceBundle, and .properties files will be sufficient for
most of your bundles, but you can create your own subclass of ResourceBundle if they are not. You must
implement two methods:


protected abstract ObjecthandleGetObject(String key)tHRows
MissingResourceException

Returns the object associated with the given key. If the key is not defined in
this bundle, it returns null, and that causes the ResourceBundle to
check in the parent (if any). Do not throw
MissingResourceException unless you check the parent instead of
letting the bundle do it. All the "get" methods are written in terms of this one
method.

public abstract EnumerationgetKeys()

Returns an Enumeration of the keys understood by this bundle, including
all those of the parent.

Exercise 24.1: Get GlobalHello to work with the example locales. Add some more locales, using
ListResourceBundle, .properties files, and your own specific subclass of ResourceBundle.


24.3. Currency


Currency encoding is highly sensitive to locale, and the java.util.Currency class helps you properly
format currency values. You obtain a Currency object from one of its static getInstance methods, one
of which takes a Locale object while the other takes a currency code as a String (codes are from the ISO
4217 standard).


The Currency class does not directly map currency values into localized strings but gives you information
you need to do so. The information at your disposal is


public StringgetSymbol()

Returns the symbol of this currency for the default locale.

public StringgetSymbol(Locale locale)

Returns the symbol of this currency for the specified locale. If there is no
locale specific symbol then the ISO 4217 currency code is returned. Many
currencies share the same symbol in their own locale. For example, the $
symbol represents U.S. dollars in the United States, Canadian dollars in
Free download pdf