Most programs contain data that is locale-sensitive. The following are all examples of locale-sensitive data:
A global program isolates these items and localizes them--that is, provides a valid version of each item for each supported locale. The JDK provides several classes that make managing locale-sensitive data a breeze.
- labels for GUI elements
- date and time displays
- numbers, including percentages and money
- error messages
To isolate locale-sensitive data you put them into resource bundles. The JDK provides three resource bundling classes to help you do this:
The base class, ResourceBundle, is an abstract class that represents a bundle of locale-sensitive data. ResourceBundle has two subclasses: ListResourceBundleand PropertyResourceBundle. ListResourceBundle is an abstract class that stores its resources in a list of key-value pairs (an array whose elements are two-element subarrays). PropertyResourceBundle uses a Propertiesobject to manage its resources. Note that while ListResourceBundle is abstract, PropertyResourceBundle is not.
Typically, to create a resource bundles, you either:
You can store any object, strings, numbers, images, sounds, whatever, in a resource bundle. The resources are retrieved by key. For each locale that your program supports, you create another version of the same resource bundle. You localize the data stored in these locale-specific versions of the bundles for that locale.
- subclass ListResourceBundle and provide the key-value pairs for the list, or
- provide a properties file that contains key-value pairs and use a PropertyResourceBundle object.
Like other classes, when you subclass ListResourceBundle you have to give the class a name. Also, you must name the properties file that you use with a PropertyResourceBundle. In both cases, you can give your resource bundles any base name you like that conforms to Java's naming restrictions and conventions.
However, each locale-specific version of that resource bundle must be derived from the base name in a specific, well-defined way. The locale-specific identifiers in the resource bundle name depend on the process used by the system to locate and load resource bundles into your program. Thus it makes sense to talk about Loading Resource Bundles, which includes details on how to name the locale-specific versions of them, before getting into the details of how to create them.
Once you understand how resource bundles are loaded into your program, and how that affects their names, you can dive into the detail of writing your own resource bundles. Storing and Accessing Strings in Resource Bundles describes the resource bundles used by AroundTheWorld to manages its text data and their localized variants. Storing and Accessing Other Objects in Resource Bundles describes the resource bundles used by AroundTheWorld to manages its numeric, image, and sound data and their localized variants.