Formatting messages with variable content

原创 2005年05月28日 11:32:00
Internationalization is the process of designing an application to work with multiple languages and in regions around the world. This not only involves translating text labels to other languages. It also also means displaying information such as dates and times in a format appropriate for that particular region of the globe.

The first step involved in internationalizing text labels and messages is to move everything into resource bundles. For each quoted string you want the user to see, you create an entry in a resource bundle. Then, you change the code to dynamically look up the text label or message based on the locale of the user. When you do this correctly, a user in the United States might see Help as the label for a help menu, while a Spanish user would see Ayuda.

This technique works perfectly well for straight text-to-text translations, where you are always displaying a "whole" message. However this technique doesn't work for compound messages, where you need to combine several pieces of a message into one longer message. For instance, consider the following message:

   Hello, John. Good luck.

You might think that you could simply use string concatenation, and build the compound message by appending multiple strings together:

     "Hello, " +
     name +
     ". Good luck.")

You might also assume that you could localize the compound message by moving the Hello and Good luck strings into resource bundles. This might work, but what happens when you get to a language where the form of the greeting becomes something like:

   Hello and good luck, John.

You could break up the resource bundle strings into a prefix part before the name, and a suffix part after the name. But this complicates things for translators because they must know what pieces go together. A better approach is to have one text string, with a variable holder in the middle for the name.

For English, that string might be:

   Hello, {0}. Good luck.

Seeing that whole string, a translator for Spanish might realize it is better to put hello and good luck together, as follows:

   Hola y buena suerte, {0}.

When it's time to actually display the message, the MessageFormat class of the java.text package can be used to replace the variables. MessageFormat takes a set of objects, formats them, and inserts the formatted strings into a pattern. The pattern could be something like "Hello, {0}. Good luck."

To use MessageFormat, you start by creating a formatter:

   String pattern = ...; // from bundle
   Locale aLocale = ...; // the Locale

   MessageFormat formatter = new MessageFormat(pattern);

For each pattern, you could create different MessageFormat objects. However you can also reuse the MessageFormat object with another pattern by calling the applyPattern method with the new pattern template. Remember to do this after changing locales:


After you have the formatter, you need to generate the output message. To do this, you pass in an array of arguments, where each {#} in the pattern is replaced, based on its index in the array. For instance, a one element array is needed for the pattern "Hello, {0}. Good luck." The one element in the array contains the text that will be inserted at position 0 in the string. Here's an example -- it's a one element array that contains the string "John" for insertion into the previous pattern:

  Object messageArgs[] = {"John"};

To generate the output, you call the format method of MessageFormat, specifying the message arguments:


The following program, HelloGoodLuck, demonstrates the use of MessageFormat. To keep things simple, the program doesn't use resource bundles:

   import java.text.*;
   import java.util.*;

   public class HelloGoodLuck {
     public static void main(String args[]) {
       String pattern = "Hello, {0}. Good luck.";
       Locale aLocale = Locale.US;
       MessageFormat formatter = new MessageFormat(
                                   pattern, aLocale);
       Object messageArgs[] = {"John"};
       // Pass in command line args
       if (args.length != 0) {

The HelloGoodLuck program produces a second message if you pass in a name on the command line. If you run the program with the following command:

   java HelloGoodLuck Spot

You should see the output:

   Hello, John. Good luck.
   Hello, Spot. Good luck.

Using MessageFormat is not limited to text substitution. You can also use it to format numbers and dates, that is, without having to use the NumberFormat and DateFormat classes. The javadoc for the MessageFormat class describes all the support available.

After the argument index part of {#}, you can specify a format type and a style (separated by commas). For instance, in the case of a date, you can specify a short, medium, long, or full to map to the DateFormat constants. If the argument type is a Date, and the MessageFormat maps that argument to "{1,date,long}", you would see the long format for a date displayed (in a format appropriate for the locale). You can also display dates with a "time" type, using the same short, medium, long, full options. For a number, the available styles include integer, currency, and percent. If you don't like the built-in styles, and know the pattern strings of SimpleDateFormat and DecimalFormat, you can also specify those directly.

To demonstrate, the following MessageFormat pattern uses time, date, and number:

   At the tone, the time is now {0, time, short}
   on {0, date, long}.
   You now owe us {1, number, currency}.

If you then provided a Date and Number as the input argument, it would generate output for US-English and German locales.

Click Here to Open New Window

And here is the program that produces that output. To keep the demonstration simple, resource bundles were not used. However, the strings in the pattern and germanPattern variables in the program should be located in resource bundles.

   import java.text.*;
   import java.util.*;
   import java.awt.*;
   import javax.swing.*;

   public class ExtendedFormat {
     public static void main(String args[]) {
       String pattern =
         "At the tone, the time is now {0, time, short}" +
         " on {0, date, long}." +
         " You now owe us {1, number, currency}.";
       String germanPattern =
         "Beim Zeitton ist es {0, time, short} Uhr" +
         " am {0, date, long}." +
         " Sie schulden uns jetzt {1, number, currency}.";
       StringWriter sw = new StringWriter(100);
       PrintWriter out = new PrintWriter(sw, true);
       MessageFormat formatter =
                 new MessageFormat(pattern, Locale.US);
       Object messageArgs[] =
                     {new Date(), new Double(9000.12)};
       // Need to reset pattern after changing locales
       // Put output in window
       JFrame frame = new JFrame("Extended Format");
       JTextArea ta = new JTextArea(sw.toString());
       JScrollPane pane = new JScrollPane(ta);
                            pane, BorderLayout.CENTER);
       frame.setSize(500, 100);;

There is much more to properly internationalizing your applications than using MessageFormat. For more information on the use of resource bundles (where all these string patterns should come from), see the May 21, 1998 Tech Tip, "Resource Bundles". Also, for more information on formatting date and time strings, see the June 24, 2003 Tech Tip, "Internationalizing Dates, Times, Months, and Days of the Week".


EF6 SQL Logging – Part 2: Changing the content/formatting

In part 1 we saw how DbContext.Database.Log can be used to easily log SQL to the console or some o...
  • lxhjh
  • lxhjh
  • 2014年03月19日 09:53
  • 1341

How to with 7bit encoding in concatenated sms messages

Where11- udl05- udhl00- IE03- IEL03- reference number03 - total number of SMS03 - 3/3 smsFill bits h...

Send ActionScript Worker Messages 6x Faster With Mutex

While ActionScript Workers made their debut in Flash Player 11.4, the Mutex class didn’t arrive unti...

Messages: There is no Action mapped for namespace [/] and action name [] associated with context pat

经过一段时间对Struts 2.0 的学习,以下是手动导包到 WEB-INF/lib ,也就是说相关资料从

Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable

本栏目(Machine learning)包括单参数的线性回归、多参数的线性回归、Octave Tutorial、Logistic Regression、Regularization、神经网络、机器学...

Dynamics of Bodies with Time-Variable Mass

  • 2016年09月21日 17:51
  • 5.1MB
  • 下载

Linear regression with one variable

  • 2016年05月30日 23:54
  • 369KB
  • 下载

Andrew Ng机器学习-Linear Regression with one variable

这一讲主要是针对单变量的线性回归来讲两个基本概念:损失函数(cost function)、梯度下降(Gradient Descent)1 Cost Function 定义如下: 左图为cost...
您举报文章:Formatting messages with variable content