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".

Leetcode 482. License Key Formatting 序列号格式 解题报告

1 解题思想这题目就是让规整格式的。 输入了一些数字字母还有破折号混合的字符串序列号,和一个K 现在要求将这个序列号,去除原来的破折号格式,整理成每K个字母数字使用一个破折号的格式,如果不能每组都...
  • MebiuW
  • MebiuW
  • 2017年01月13日 16:14
  • 1098


content:// content://
  • bruse1990
  • bruse1990
  • 2014年07月07日 19:32
  • 951

String Formatting in C#

Every object has a method called ToString that returns a string representation of the object. The To...
  • 21aspnet
  • 21aspnet
  • 2007年03月20日 10:55
  • 2224

482. License Key Formatting

Now you are given a string S, which represents a software license key which we would like to format....
  • kakitgogogo
  • kakitgogogo
  • 2017年01月14日 12:25
  • 480

Excel条件格式化(conditional formatting)应用

条件格式化顾名思义就是根据条件对单元格进行格式化(填充,字体等)。 比如我们有一个学生成绩表,根据需要我们要把成绩按“不及格,60且90”进行颜色标注。...
  • claroja
  • claroja
  • 2016年12月09日 15:09
  • 1493

IFC(Inline formatting contexts)学习

翻译来自: 在IFC中,盒子依次水平排列,从它的包含块的最顶端开始。水平方向的ma...
  • u010552788
  • u010552788
  • 2016年10月17日 17:48
  • 379

leetcode 482. License Key Formatting(Key码格式化)(C++和Java)

leetcode 482. License Key Formatting(Key码格式化)(C++和Java)
  • Shauna_Wu
  • Shauna_Wu
  • 2017年12月13日 19:50
  • 107

go tool vet是你的好朋友

go tool vet是你的好朋友,不要忽视它。 转自: vet是一个优雅的工具,每个Go开发者都要知道并会使用...
  • yueguanyun
  • yueguanyun
  • 2017年03月21日 22:09
  • 520

Excel 中条件格式化单元格 conditional formatting

Due to my companys more and more strict Internet access policy, I can no longer access Live Spaces t...
  • gracexu
  • gracexu
  • 2007年02月12日 22:22
  • 1433

uva 709 &poj 1093 - Formatting Text (dp之小白告一段落)

709 - Formatting Text Writings e-mails is fun, but, unfortunately, they do not look very nice, mai...
  • u010228612
  • u010228612
  • 2014年03月06日 21:15
  • 1041
您举报文章:Formatting messages with variable content