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.
Code
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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Lorem Ipsum GeneratorGenerate deterministic placeholder text with a chosen number of paragraphs, sentences, and words per sentence.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- How do you run MessageFormat Placeholder Templates?
- This is a complete program, not a library excerpt — execution starts at the top.
Report a problem with this exampleOpens a pre-filled issue on GitHub.