Formatting |
By stepping through a sample program, this section demonstrates how to internationalize a compound message. The sample program makes use of theMessageFormat
class. The full source code for this program is in the file called MessageFormatDemo.java.1. Identify the Variables in the Message
Suppose you want to internationalize the following message:Notice that we've underlined the variable data, and have identified what kind of objects will represent this data.At 1:15 PM on April 13, 1998, we detected 7 spaceships on the planet Mars. ^ ^ ^ ^ | | | | Date Date Number String2. Isolate the Message Pattern in a ResourceBundle
Store the message in aResourceBundle
namedMessageBundle
:ThisResourceBundle messages = ResourceBundle.getBundle("MessageBundle",currentLocale);ResourceBundle
is backed by a properties file for eachLocale
. Since theResourceBundle
is calledMessageBundle
, the properties file for U.S. English is namedMessageBundle_en_US.properties
. The contents of this file is as follows:The first line of the properties file contains the message pattern. If you compare this pattern with the message text shown in step 1, you'll see that an argument enclosed in curly braces replaces each variable in the message text. Each argument starts with a digit called the argument number, which matches the index of an element in antemplate = At {2,time,short} on {2,date,long}, we detected \ {1,number,integer} spaceships on the planet {0}. planet = MarsObject
array that holds the argument values. Note that in the pattern, the argument numbers are not in any particular order. You can place the arguments anywhere in the pattern. The only requirement is that the argument number has a matching element in the array of argument values. The next step discusses the argument value array, but first, let's look at each of the arguments in the pattern. The following table provides some details about the arguments:
For a full description of the argument syntax, see the API documentation for the
Argument Description {2,time,short} The time portion of a Date
object. The "short" style specifies
theDateFormat.SHORT
formatting style.{2,date,long} The date portion of a Date
object. The sameDate
object is used for
both the date and time variables. In theObject
array of arguments
the index of the element holding theDate
object is 2.{1,number,integer} A Number
object, further qualified with the "integer" number style.{0} The String
in theResourceBundle
that corresponds to the "planet" key.MessageFormat
class.3. Set the Message Arguments
The following lines of code assign values to each argument in the pattern. The indexes of the elements in themessageArguments
array match the argument numbers in the pattern. For example, theInteger
element at index 1 corresponds to the{1,number,integer}
argument in the pattern. Because it must be translated, theString
object at element 0 will be fetched from theResourceBundle
with thegetString
method. Here is the code that defines the array of message arguments:Object[] messageArguments = { messages.getString("planet"), new Integer(7), new Date() };4. Create the Formatter
Next, create aMessageFormat
object. Set theLocale
because the message containsDate
andNumber
objects, which should be formatted in a locale-sensitive manner.MessageFormat formatter = new MessageFormat(""); formatter.setLocale(currentLocale);5. Format the Message Using the Pattern and the Arguments
This step shows how the pattern, message arguments, and formatter all work together. First, fetch the patternString
from theResourceBundle
with thegetString
method. The key to the pattern is "template." Pass the patternString
to the formatter with theapplyPattern
method. Then, format the message using the array of message arguments by invoking theformat
method. TheString
returned by theformat
method is ready to be displayed. All of this is accomplished with just two lines of code:formatter.applyPattern(messages.getString("template")); String output = formatter.format(messageArguments);6. Run the Demo Program
The demo program prints the translated messages for the U.S. and German locales. Note that the date and time variables are properly formatted:currentLocale = en_US At 1:15 PM on April 13, 1998, we detected 7 spaceships on the planet Mars. currentLocale = de_DE Um 13.15 Uhr am 13. April 1998 haben wir 7 Raumschiffe auf dem Planeten Mars entdeckt.
Formatting |