You do not need to replicate elements in a resource bundle if they exist in
another bundle within the resource bundle hierarchy. Example 4-6
demonstrates the lookup of the key "Hello." Examples 4-7, 4-8, and 4-9 are
the resource bundles used by Example 4-6.
Example 4-6. Demonstrates That You Do Not Have to Replicate Keys and Values in All Resource Bundles
import java. util. Enumeration;
import java. util. Locale;
import java. util. ResourceBundle;
import java. util. MissingResourceException;
public class HelloResourceBundleExample {
public static void main( String [] argv) {
try {
Locale frenchLocale = new Locale(" fr", "FR");
ResourceBundle rb =
ResourceBundle. getBundle(" HelloResourceBundle", frenchLocale);
System. out. println( rb. getString(" Hello"));
System. out. println( rb. getString(" Goodbye"));
} catch (MissingResourceException mre) {
mre. printStackTrace();
}
}
}
Example 4-7. Fallback Resource Bundle
import java. util. Enumeration;
import java. util. ResourceBundle;
import java. util. StringTokenizer;
public class HelloResourceBundle extends ResourceBundle {
private String keys = "Hello Goodbye";
public Object handleGetObject( String key) {
if (key. equals(" Hello")) return "Hello";
if (key. equals(" Goodbye")) return "Goodbye";
return null;
}
public Enumeration getKeys() {
StringTokenizer keyTokenizer = new StringTokenizer( keys);
return keyTokenizer;
}
}
Example 4-8. French Resource Bundle Containing the Key "Hello"
import java. util. Enumeration;
import java. util. ResourceBundle;
import java. util. StringTokenizer;
public class HelloResourceBundle_ fr extends HelloResourceBundle {
public Object handleGetObject( String key) {
if (key. equals(" Hello")) return "Bonjour";
return null;
}
}
Example 4-9. French Resource Bundle Containing the Key "Goodbye"
import java. util. Enumeration;
import java. util. ResourceBundle;
import java. util. StringTokenizer;
public class HelloResourceBundle_ fr_ FR extends HelloResourceBundle_ fr {
public Object handleGetObject( String key) {
if (key. equals(" Goodbye")) return "Au Revoir";
return null;
}
}
Example 4-9 uses the locale "fr_ FR," so the search for a
resource matching the key of "Hello" starts in HelloResourceBundle_
fr_ FR. The key does not exist in this bundle, so the search
proceeds up the hierarchy. The key is found in HelloResourceBundle_
fr, and the value is printed to the screen. Likewise, the search for
a resource matching the key of "Goodbye" starts in
HelloResourceBundle_ fr_ FR. The search begins and ends here,
since the key exists in this resource bundle.
The first time you search for a particular resource bundle, Java starts a
lookup process and either returns the specific resource bundle you
requested or throws a MissingResourceException if the resource
is not found. Searching for a particular resource bundle (after that bundle
was found) results in the bundle being cached in memory. Java stores the
resource bundle in a hashtable for quick lookup. Requests made for a
particular resource bundle at a later point in your application result in
use of the cached bundle. Had such design considerations not been made,
this situation could have been a huge bottleneck in programs that rely on
resource bundle facilities. Think of how slow programs would be if each
time you request a resource from a bundle, the system must go through the
steps of searching the resource bundle hierarchy, loading the bundle from
disk, and returning the data for a particular resource.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|