LIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK") {"OkKey", "OK"}, {"CancelKey", "Cancel"}, // END OF MATERIAL TO LOCALIZE }; } } Keys are always
Strings. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also
Strings--"OK" and "Cancel"--but they don't have to be. The values can be any type of object.
You retrieve an object from resource bundle using the appropriate getter method. Because "OkKey" and "CancelKey" are both strings, you would use getString to retrieve them:
button1 = new Button(myResources.getString("OkKey")); button2 = new Button(myResources.getString("CancelKey"));
The getter methods all require the key as an argument and return the object if found. If the object is not found, the getter method throws a
MissingResourceException.
Besides getString, ResourceBundle also provides a method for getting string arrays, getStringArray, as well as a generic getObject method for any other type of object. When using getObject, you'll have to cast the result to the appropriate type. For example:
int[] myIntegers = (int[]) myResources.getObject("intList");
The Java Platform provides two subclasses of ResourceBundle, ListResourceBundle and PropertyResourceBundle, that provide a fairly simple way to create resources. As you saw briefly in a previous example, ListResourceBundle manages its resource as a list of key/value pairs. PropertyResourceBundle uses a properties file to manage its resources.
If ListResourceBundle or PropertyResourceBundle do not suit your needs, you can write your own ResourceBundle subclass. Your subclasses must override two methods: handleGetObject and getKeys().
ResourceBundle.Control
The {@link ResourceBundle.Control} class provides information necessaryto perform the bundle loading process by the
getBundle factory methods that take a
ResourceBundle.Control instance. You can implement your own subclass in order to enable non-standard resource bundle formats, change the search strategy, or define caching parameters. Refer to the descriptions of the class and the {@link #getBundle(String,Locale,ClassLoader,Control) getBundle}factory method for details.
Cache Management
Resource bundle instances created by the
getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached.
getBundle clients may clear the cache, manage the lifetime of cached resource bundle instances using time-to-live values, or specify not to cache resource bundle instances. Refer to the descriptions of the {@linkplain #getBundle(String,Locale,ClassLoader,Control)
getBundle factory method}, {@link #clearCache(ClassLoader) clearCache}, {@link Control#getTimeToLive(String,Locale) ResourceBundle.Control.getTimeToLive}, and {@link Control#needsReload(String,Locale,String,ClassLoader,ResourceBundle,long) ResourceBundle.Control.needsReload} for details.
Example
The following is a very simple example of a
ResourceBundle subclass,
MyResources, that manages two resources (for a larger number of resources you would probably use a
Map). Notice that you don't need to supply a value if a "parent-level"
ResourceBundle handles the same key with the same value (as for the okKey below).
// default (English language, United States) public class MyResources extends ResourceBundle { public Object handleGetObject(String key) { if (key.equals("okKey")) return "Ok"; if (key.equals("cancelKey")) return "Cancel"; return null; } public Enumeration<String> getKeys() { return Collections.enumeration(keySet()); } // Overrides handleKeySet() so that the getKeys() implementation // can rely on the keySet() value. protected Set<String> handleKeySet() { return new HashSet<String>(Arrays.asList("okKey", "cancelKey")); } } // German language public class MyResources_de extends MyResources { public Object handleGetObject(String key) { // don't need okKey, since parent level handles it. if (key.equals("cancelKey")) return "Abbrechen"; return null; } protected Set<String> handleKeySet() { return new HashSet<String>(Arrays.asList("cancelKey")); } }
You do not have to restrict yourself to using a single family of
ResourceBundles. For example, you could have a set of bundles for exception messages,
ExceptionResources (
ExceptionResources_fr,
ExceptionResources_de, ...), and one for widgets,
WidgetResource (
WidgetResources_fr,
WidgetResources_de, ...); breaking up the resources however you like.
@see ListResourceBundle
@see PropertyResourceBundle
@see MissingResourceException
@since JDK1.1