advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement


Title: Java Internationalization
ISBN: 0596000197
US Price: $ 39.95

© 2001 O'Reilly & Associates, Inc.

Reviews : Java Books :
Java Internationalization : Isolating Locale-Specific Data With Resource Bundles

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.

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs