|
|
Contents |
Locale-specific data must be tailored according to the conventions of the end-user's language and region. The text displayed by a user interface is the most obvious example of locale-specific data. For example, an application with a "Cancel" button in the U.S. will have an "Abbrechen" button in Germany. In other countries this button will have other labels. Obviously, you don't want to hardcode this button label. Wouldn't it be nice if you could automatically get the correct label for a givenLocale? Fortunately, you can, provided that you isolate the locale-specific objects in aResourceBundle.In this lesson, you'll learn how to create, load, and access
ResourceBundleobjects. If you're in a hurry to examine some coding examples, go ahead and check out the last two sections in this lesson. Then, you can come back to the first two sections to get some conceptual information aboutResourceBundleobjects.About the ResourceBundle Class
ResourceBundleobjects contain locale-specific objects. When you need a locale-specific object, you fetch it from aResourceBundle, which returns the object that matches the end-user'sLocale. In this section, we'll explain how aResourceBundleis related to aLocale. We'll also describe theResourceBundlesubclasses, and when you should use them.Preparing to Use a ResourceBundle
Before you create and load yourResourceBundleobjects, you should do a little planning. First, identify the locale-specific objects in your program. Then, organize these locale-specific objects into categories and store them in differentResourceBundleobjects accordingly.Backing a ResourceBundle with Properties Files
If your application containsStringobjects that need to be translated into different languages, you should store theseStringobjects in aPropertyResourceBundle, which is backed up by a set of properties files. Since the properties files are simple text files, they can be created and maintained by your translators. You don't have to change the source code. In this section, you'll learn how to set up the properties files that back up aPropertyResourceBundle.Using a ListResourceBundle
TheListResourceBundleclass, which is a subclass ofResourceBundle, manages locale-specific objects with a list. AListResourceBundleis backed by a class file, which means you must code and compile an new source file each time support for an additionalLocaleis needed. Therefore, you should not use aListResourceBundleto isolateStringobjects that must be translated into other languages. Instead, you use aPropertyResourceBundlebecause it is backed up by a set of editable properties files. However,ListResourceBundleobjects are useful, because unlike properties files, they can store any type of locale-specific object. By stepping through an example program, this section demonstrates how to use aListResourceBundle.
|
|
Contents |