Previous | Next | Trail Map | Internationalization | Formatting

Messages

We all like to use programs that let us know what's going on. Programs that keep us informed often do so by displaying status and error messages. Of course, these messages need to be translated so they can be understood by end-users around the world. The section, Isolating Locale-Specific Data, discussed translatable text messages. Usually, after you move a message String into a ResourceBundle, you're done. However, if you've embedded variable data in a message, you'll have to take some extra steps to prepare it for translation.

A compound message contains variable data. In the following list of compound messages, the variable data is underlined:

The disk named MyDisk contains 300 files.
The current balance of account #34-98-222 is $2,745.72.
405,390 people have visited your website since January 1, 1998.
Delete all files older than 120 days.
You might be tempted to construct the last message in the preceding list by concatenating phrases and variables:
double numDays;
ResourceBundle mssgBundle;
.
.
.
String message = mssgBundle.getString("deleteolder") +
                 numDays.toString() + 
                 mssgBundle.getString("days");
This approach works fine in English, but it won't work for languages where the verb appears at the end of the sentence. Because the word order of this message is hardcoded, your localizers won't be able to create grammatically correct tranlations for all languages.

How can you make your program localizable if you need to use compound messages? You can do so by using the MessageFormat class, which is the topic of this lesson.

First, a brief warning: Translating compound messages is difficult because the message text is fragmented. Text that is difficult to translate delays the localization process and increases costs. Therefore, use compound messages only when necessary.

Dealing with Compound Messages

A compound message may contain several kinds of variables: dates, times, strings, numbers, currencies, and percentages. To format a compound message in a locale-independent manner, you construct a pattern that you apply to a MessageFormat object.

Handling Plurals

The words in a message usually vary if both plural and singular word forms are possible. With the ChoiceFormat class, you can map a number to a word or phrase, allowing you to construct messages that are grammatically correct.


Previous | Next | Trail Map | Internationalization | Formatting