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

Your application contains a class called FooBar in the com. foocompany package. This class uses the getBundle( String baseName, Locale locale) method to obtain resource data from the FooBarTextElements class for different locales. You've also provided a number of resource bundles for text elements in the com. foocompany. resources package. If the method call in the FooBar class looks like ResourceBundle. getBundle(" FooBarTextElements", userLocale) a MissingResourceException is going to be thrown because the fully qualified name (package name and class name) needs to be passed into the getBundle() method.

The proper way to get the resource bundle in this situation is to use ResourceBundle. getBundle("com. foocompany. resources. FooBarTextElements", userLocale).

Example 4-4 illustrates this point.

Example 4-4. Proper Retrieval of a Resource Bundle in a Package

package com. foocompany; 

import java. util. Locale; 
import java. util. ResourceBundle; 
import java. util. MissingResourceException; 

public class FooBar { 
  public static void main( String [] argv) { 
    ResourceBundle textResources = null; 

    try { 
      textResources = ResourceBundle. getBundle(" FooBarTextElements", 
        Locale. getDefault()); 
    System. out. println( textResources. getString(" CompanyName")); 
  } catch (MissingResourceException e) { 
    System. out. println(" Unqualified resource bundle name"); 
  } 

    try { 
      textResources = ResourceBundle. getBundle( 
        "com. foocompany. resources. FooBarTextElements", 
        Locale. getDefault()); 
      System. out. println( textResources. getString(" CompanyName")); 
    } catch (MissingResourceException e) { 
    } 
  } 
}

Example 4-5 illustrates the resource bundle used in Example 4-4.

Example 4-5. Resource Bundle Used in Example 4-4

package com. foocompany. resources; 

import java. util. Enumeration; 
import java. util. Locale; 
import java. util. ResourceBundle; 
import java. util. StringTokenizer; 
import java. util. MissingResourceException; 

public class FooBarTextElements extends ResourceBundle { 

  private String keys = "CompanyName"; 

    public Object handleGetObject( String key) { 
      if (key. equals(" CompanyName")) return "Foo Bar Company"; 

      return null; 
    } 

    public Enumeration getKeys() { 
      StringTokenizer keyTokenizer = new StringTokenizer( keys); 

      return keyTokenizer; 
    } 
} 

Running Example 4-4 produces the following results:

C:\> java com. foocompany. FooBar 
Unqualified resource bundle name 
Foo Bar Company 

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.