MessageFormat Placeholder Templates

Build user-facing messages with MessageFormat placeholders so numbers and dates render per Locale, and single quotes need doubling to stay literal.

JavaCli Tooling

Code

snippet
import java.text.MessageFormat;
import java.util.Locale;

public class NoticeTemplates {

    public static void main(String[] args) {
        // 占位符 placeholder {0} {1}:数字子格式按 Locale 惯例渲染
        MessageFormat en = new MessageFormat("User {0} has {1,number,integer} alerts.", Locale.US);
        MessageFormat zh = new MessageFormat("用户 {0} 有 {1,number,integer} 条告警。", Locale.CHINA);

        System.out.println(en.format(new Object[] { "ada", 3 }));
        System.out.println(zh.format(new Object[] { "ada", 3 }));

        // 单引号是模板特殊字符:成对写出 '' 才输出一个字面量引号
        MessageFormat quoted = new MessageFormat("Disk ''{0}'' is {1} full.", Locale.US);
        System.out.println(quoted.format(new Object[] { "sda1", "90%" }));
    }
}

How it works

MessageFormat Placeholder Templates keeps everything in the class NoticeTemplates and the function main.

Statements that matter

MessageFormat en = new MessageFormat("User {0} has {1,number,integer} alerts.", Locale.US);
MessageFormat zh = new MessageFormat("用户 {0} 有 {1,number,integer} 条告警。", Locale.CHINA);
MessageFormat quoted = new MessageFormat("Disk ''{0}'' is {1} full.", Locale.US);

Interactive Tools for This Topic

FAQ

How do you run MessageFormat Placeholder Templates?
This is a complete program, not a library excerpt — execution starts at the top.