| This Chapter | |
| - | Chapter 7: Internationalization |
| - | New Supported Locales |
| - | Locale Sensitive Services SPI |
| - | Resource Bundle Enhancement |
| - | ResourceBundle.Control |
| - | Summary |
There are several new methods added to the java.util.ResourceBundle class in Mustang. First and foremost, you can now pass a ResourceBundle.Control object to the getBundle method that returns an instance of ResourceBundle. Recall that ResourceBundle itself is an abstract class. I’ll explain about the ResourceBundle.Control class in a later section.
To retrieve a value from a resource bundle, you can use the ResourceBundle class’s getString method:
public final java.lang.String getString(java.lang.String key)
This method throws a MissingResourceException if the specified key does not exist, and there was no way you could check if a key existed prior-to Mustang. Fortunately, there’s now a containsKey method for this purpose:
public boolean containsKey(java.lang.String key)
In Mustang you can even retrieve all the keys as a Set using one of these two methods, keySet and handleKeySet:
public SetkeySet() protected Set handleKeySet()
The difference between the two methods is keySet returns all keys in this ResourceBundle and its parent bundles whereas handleKeySet returns the keys in this ResourceBundle only.
For improved performance, ResourceBundle also caches bundles that have been loaded. Mustang programmers can clear these caches using one of these methods:
public static void clearCache()
Removes all resource bundles from the cache loaded using the caller’s class loader.
public static void clearCache(java.lang.ClassLoader loader)
Removes bundles from the cache loaded using the specified class loader.
The following example create a resource bundle and then prints all the keys in the resource bundle. There are two properties files used, the default one (in Listing 7.4) and the German version (Listing 7.5).
Listing 7.4: The MyResources.properties file
okKey = OK cancelKey = Cancel submitKey = Submit
Listing 7.5: The MyResources_de.properties file
cancelKey = Abbrechen
Notice that there are three key/value pairs in the default properties file, but only one in the second file. Therefore, if the German locale is used and a certain key is not found in the MyResources_de.properties file, the ResourceBundle will try the default.
The RBPropDemo class in Listing 7.6 creates a bundle using the German locale and calls the new methods in Java 6.
Listing 7.6: Using the latest ResourceBundle class
package rbprop;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.ResourceBundle.Control;
public class RBPropDemo {
public static void main(String[] args){
// Removes all bundles from the cache
ResourceBundle.clearCache();
String bundleName = "rbprop.MyResources";
// Create resource bundle
ResourceBundle myResources = ResourceBundle.getBundle(
bundleName, Locale.GERMAN);
// Print the key's value
System.out.println("Key's values:");
System.out.println(myResources.getString("okKey"));
System.out.println(myResources.getString("cancelKey"));
System.out.println(myResources.getString("submitKey"));
System.out.println("\nChecking okKey in resource bundle:");
if (myResources.containsKey("okKey")) {
System.out.println("okKey exists! "
+ " Value = " + myResources.getString("okKey"));
} else {
System.out.println("The key Doesn't Exist");
}
// Returns all keys in this resource bundle and
// its parent bundles
System.out.println("\nGet a set of keys:");
Set keySet = myResources.keySet();
Object[] keys = keySet.toArray();
for (int i = 0; i < keys.length; i++) {
System.out.println("Key " + (i + 1) + " = " + keys[i]);
}
}
}
If you run the RBPropDemo class, here is what you get:
Key's values: OK Abbrechen Submit Checking okKey in resource bundle: okKey exists! Value = OK Get a set of keys: Key 1 = okKey Key 2 = submitKey Key 3 = cancelKey