JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
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 individual text message. There are 3 ways to map key names to the localized text messages:
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) HerongYang.com. All Rights Reserved. */ 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<String> getKeys() { Vector<String> keys = new Vector<String>(); keys.addElement("NO_FILE"); keys.addElement("FILE_EMPTY"); return keys.elements(); } }
Note that:
2. Mapping ErrorMessage key names to localized text messages for the fr locale:
/* ErrorMessage_fr.java * Copyright (c) HerongYang.com. All Rights Reserved. */ 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:
3. Mapping ErrorMessage key names to localized text messages for the fr and CA locale:
/* ErrorMessage_fr_CA.java * Copyright (c) HerongYang.com. All Rights Reserved. */ 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:
More localization key names and localized text messages will be defined in sections below.
Table of Contents
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
►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
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
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 - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs