Java Reference
In-Depth Information
parts of the message are to be placed. For example, one resource bundle might
contain the message: “Error at line {0} of file {1}.” And another resource bundle
might contain a “translation” that looks like this: “Erreur: {1}: {0}.”
To use such a localized message, you create a MessageFormat object from the
static part of the message and then call its format() method, passing in an array of
the values to be substituted. In this case, the array contains an Integer object that
specifies the line number and a String object that specifies the filename. The Mes-
sageFormat class knows about other Format classes defined in java.text .Itcre-
ates and uses NumberFormat objects to format numbers and DateFormat objects to
format dates and times. In addition, you can design messages that create Choice-
Format objects to convert from numbers to strings. This is useful when working
with enumerated types, such as numbers that correspond to month names, or
when you need to use the singular or plural form of a word based on the value of
some number.
Example 7-5 demonstrates this kind of MessageFormat usage. It is a convenience
class with a single static method for the localized display of exception and error
message. When invoked, the code attempts to load a ResourceBundle with the
basename “Errors”. If found, it looks up a message resource using the class name
of the exception object that was passed. If such a resource is found, it displays the
error message. An array of five values is passed to the format() method. The
localized error message can include any or all of these arguments.
The LocalizedError.display() method defined in this example was used in
Example 7-2 at the beginning of this chapter. The default Err ors.pr operties resource
bundle used in conjunction with this example is shown following the code listing.
Error message display for the program is nicely internationalized. Porting the pro-
gram's error message to a new locale is simply a matter of translating (localizing)
the Err ors.pr operties file.
Example 7−5: LocalizedError.java
package com.davidflanagan.examples.i18n;
import java.text.*;
import java.io.*;
import java.util.*;
/**
* A convenience class that can display a localized exception message
* depending on the class of the exception. It uses a MessageFormat,
* and passes five arguments that the localized message may include:
* {0}: the message included in the exception or error.
* {1}: the full class name of the exception or error.
* {2}: a guess at what file the exception was caused by.
* {3}: a line number in that file.
* {4}: the current date and time.
* Messages are looked up in a ResourceBundle with the basename
* "Errors", using a the full class name of the exception object as
* the resource name. If no resource is found for a given exception
* class, the superclasses are checked.
**/
public class LocalizedError {
public static void display(Throwable error) {
ResourceBundle bundle;
// Try to get the resource bundle.
Search WWH ::




Custom Search