FileDocCategorySizeDatePackage
MessageFormatDemo.javaAPI DocExample29591Wed Apr 19 11:19:02 BST 2000None

MessageFormatDemo

public class MessageFormatDemo extends DemoApplet

Pattern formats are used to put together sequences of strings, numbers, dates, and other formats to create messages. The pattern formatters facilitate localization because they prevent both hard-coding of message strings, and hard-coding of the concatenation sequence for portions of message strings. This means localizers can change the content, format, and order of any text as appropriate for any language.

About the Pattern Format  Formatting Arguments
Supporting Multiple Choices


About the Pattern Format

The pattern is a sequence of text and arguments, all of which can be edited. The positions of the arguments within the sequence are indicated by a "%" followed by a digit n identifying the argument. The pattern in the demo applet has three arguments, numbered 0, 1, and 2. The arguments can appear in any order within the sequence, which can be edited and modified.

To See This... Do This...
You can move arguments freely within the sequence or delete arguments. You can also edit or translate any of the unformatted text.
1. Move the string "on %2" from the end of the pattern to the front and correct the capitalization
Translations are provided in the demo applet for the G7 countries.
1. Pull down the Locale menu
2. Try several different locales with the up and down arrow keys (on Windows) or the mouse button (on Macintosh)

Note: To add a real percentage character to the pattern, enter "%%".


Formatting Arguments

The arguments can be either simple text strings or formattable, localizable objects. The pattern in the demo applet, for example, includes a date, an unformatted string, and a more complex format called a choice format (described below). You can edit these arguments at will. When localizing, you can also select any format to be associated with an argument.

To See This... Do This...
You can modify the value of any argument.
1. Select the "3" in argument 2 and change it to another number —the formatted date adjusts to the new value
You can change the format of any argument, and can specify "unformatted" arguments that are not localized.
1. Change the format type for argument 2 to None
2. Try different locales and notice that the date does not reformat for different locales
3. Return the format back to Date and try different locales again. The date reformats.

Supporting Multiple Choices

Choice formats, like that used for argument 0, let localizers create more natural messages, avoiding phrases like "3 file(s)". As shown here, the correct text can be chosen for different numbers. This works even in more complicated contexts, such as Slavic languages which have more than one plural format based on the number involved. A particular choice is chosen based on the value of the argument, and each choice can be edited individually.

Look also at the format for the choice associated with values of 2 and higher, "%0|3". The vertical bar indicates that the choice uses the value for argument 0, but the format for argument 3 (in this case, a Number). This allows for a degree of flexibility in using different formats.

To See This... Do This...
You can edit the value of any of the choice options.
1. Select the Choice String "no files" and type in "not a single file"
2. Select the value for argument 0 and type in "0"
You can establish different choices for parameters based on the value of an argument, so that strings are substituted that agree numerically.
1. Select the value for argument 0 and type in "0", then replace it with a "1", and then with a "2". The string changes correspondingly.
2. Select argument 0 and return the value to "0". Choose the French or German locale and notice that it makes correct substitutions in any language.
You can add as many alternatives as you need for different value ranges.
1. Select the U.S. English locale
2. Select the empty choice value box (under the "2") and type in "10"
3. Type in "many files" in the corresponding Choice Strings field
4. Enter a number larger than10 for the value of argument 0—" many files" is substituted in the resulting message

You can type in other text in the pattern, arguments, or choices fields to see different formatting behaviors. Try it out!

see
java.util.Format
see
java.util.MessageFormat
version
1.1 11/23/96
author
Laura Werner, Mark Davis

Fields Summary
Constructors Summary
Methods Summary
public java.awt.FramecreateDemoFrame(DemoApplet applet)
This creates a MessageFormatFrame for the demo applet.

        return new MessageFormatFrame(applet);
    
public static voidmain(java.lang.String[] argv)
The main function which defines the behavior of the MessageFormatDemo applet when an applet is started.

        DemoApplet.showDemo(new MessageFormatFrame(null));