JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

java.util.ResourceBundle - Resource Bundle Abstract Class

This section provides a tutorial example on how to create resource bundles (localization key names with their localized text messages) as subclasses of the java.util.ResourceBundle abstract class.

The ResourceBundle class, java.util.ResourceBundle, is an abstract class providing a foundation for subclasses to represent a specific map of resource keys and the associated localized resource objects.

Resource bundles are commonly used to localize the text messages in an application, where a key name is assigned to each individula text message. There are 3 ways to map key names to the localized text messages:

  • Implement a series of subclasses of ResourceBundle class
  • Implement a series of subclasses of ListResourceBundle class
  • Implement a series of property files, and let the system to automatically create objects of PropertyResourceBundle class

First let's define some localization key names and localized text messages for some error messages using subclasses of java.util.ResourceBundle class:

1. Mapping ErrorMessage key names to localized text messages for the default locale:

/**
 * ErrorMessage.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
public class ErrorMessage extends ResourceBundle {
   public Object handleGetObject(String key) {
      if (key.equals("NO_FILE")) return "File doesn't exist.";
      if (key.equals("FILE_EMPTY")) return "File is empty.";
      return null;
   }
   public Enumeration getKeys() {
      Vector keys = new Vector();
      keys.addElement("NO_FILE");
      keys.addElement("FILE_EMPTY");
      return keys.elements();
   }
}

Note that:

  • Only the two required methods handleGetObject() and getKeys() are implemented. They are declared as abstract in the ResourceBundle class.

2. Mapping ErrorMessage key names to localized text messages for the fr locale:

/**
 * ErrorMessage_fr.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
public class ErrorMessage_fr extends ErrorMessage {
   public Object handleGetObject(String key) {
      if (key.equals("NO_FILE")) return "Document n'exist pas.";
      if (key.equals("FILE_EMPTY")) return "Rien.";
      return null;
   }
}

Note that:

  • I am extending ErrorMessage_fr from ErrorMessage, so I don't have to repeat the handleGetObject() method.
  • The resource bundle name is the class name of the default locale. The class names for other locales must have a sufix of language code.

3. Mapping ErrorMessage key names to localized text messages for the fr and CA locale:

/**
 * ErrorMessage_fr_CA.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
public class ErrorMessage_fr_CA extends ErrorMessage {
   public Object handleGetObject(String key) {
      if (key.equals("NO_FILE")) return "Dossier n'exist pas.";
      return null;
   }
}

Note that:

  • The class names have the same hierarchical structure as the locales. Default locale is at the top level; language specific locales are at the middle level; language and country specific locales are at the bottom level.
  • For a given key and a given locale, the lookup mechanism searches in the class that matche the locale first. If failed, it will continue with the class at the parent level. In my example, for the key, "FILE_EMPTY", and the locale, "fr and CA", the lookup mechnism will search the ErrorMessage_fr_CA class first, then the ErrorMessage_fr class, then the ErrorMessage class. So the resulting object will be the String, "Rien." from the ErrorMessage_fr class.

More localization key names and localized text messages will be defined in sections below.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.3.1 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

Locales, Localization Methods and Resource Bundles

 java.util.Locale - Localization and Internationalization

 Locale Sensitive Operations

java.util.ResourceBundle - Resource Bundle Abstract Class

 java.util.ListResourceBundle - Resource Bundles as Lists

 java.util.PropertyResourceBundle - Resource Bundles as Properties Files

 java.util.ResourceBundle.getBundle() - Retrieving Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
java.util.ResourceBundle - Resource Bundle Abstract Class