Java Reference
In-Depth Information
Discussion
In English, for example, we say “file not found.” But in other languages the word order is
different: the words for “not found” might need to precede the word for “file.” Java accounts
for this using the
MessageFormat
class. Suppose we want to format a message as follows:
$
java i18n.MessageFormatDemoIntl
At 3:33:02 PM on 01-Jul-00, myfile.txt could not be opened.
$
java -Duser.language=es i18n.MessageFormatDemoIntl
A 3:34:49 PM sobre 01-Jul-00, no se puede abrir la fila myfile.txt.
$
The
MessageFormat
in its simplest form takes a format string with a series of numeric in-
dexes and an array of objects to be formatted. The objects are inserted into the resulting
string, where the given array index appears. Here is a simple example of a
MessageFormat
in
action:
public
public class
class
MessageFormatDemo
MessageFormatDemo
{
static
static
Object
[]
data
= {
new
new
java
.
util
.
Date
(),
"myfile.txt"
,
"could not be opened"
};
public
public static
void
main
(
String
[]
args
) {
String result
=
MessageFormat
.
format
(
"At {0,time} on {0,date}, {1} {2}."
,
data
);
System
.
out
.
println
(
result
);
static
void
}
}
But we still need to internationalize this, so we'll add some lines to our widget's properties
files. In the default (English) version:
# These are for MessageFormatDemo
#
filedialogs.cantopen.format=At {0,time} on {0,date}, {1} could not be opened.
In the Spanish version, we'll add the format as follows: