THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

we get


Italie


the French name for Italy.


The methods getISO3Country and getISO3Language return three-character ISO codes for the
country and language of the locale, respectively.


24.2. Resource Bundles


When you internationalize code, you commonly have units of meaningsuch as text or soundsthat must be
translated or otherwise made appropriate for each locale. If you put English text directly into your program,
localizing that code is difficultit requires finding all the strings in your program, identifying which ones are
shown to users, and translating them in the code, thereby creating a second version of your program for, say,
Swahili users. When you repeat this process for a large number of locales the task becomes a nightmare.


The resource bundle classes in java.util help you address this problem in a cleaner and more flexible
fashion. The abstract class ResourceBundle defines methods to look up resources in a bundle by string
key and to provide a parent bundle that will be searched if a bundle doesn't have a key. This inheritance
feature allows one bundle to be just like another bundle except that a few resource values are modified or
added. For example, a U.S. English bundle might use a U.K. English bundle for a parent, providing
replacements for resources that have different spelling. ResourceBundle provides the following public
methods:


public final StringgetString(String key)tHRows
MissingResourceException

Returns the string stored in the bundle under the given key.

public final String[]getStringArray(String key)throws
MissingResourceException

Returns the string array stored in the bundle under the given key.

public final ObjectgetObject(String key)throws
MissingResourceException

Returns the object stored in the bundle under the given key.

public abstract EnumerationgetKeys()

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

Each resource bundle defines a set of string keys that map to locale-sensitive resources. These strings can be
anything you like, although it is best to make them mnemonic. When you want to use the resource you look it
up by name. If the resource is not found a MissingResourceException is thrown. The resources
themselves can be of any type but are commonly strings, so the getString methods are provided for

Free download pdf