惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - daniel-shen

精通EJB-概述 硬件基础知识大全 教程:关于XMLBeans的初步 在客户端进行xslt对xml的xuan渲染 jaxp笔记2007-4-17 6.ZK用户接口标记语言 5.事件的监听和处理 - daniel-shen - 博客园 4.组件的生命周期 3.zk体系结构 "tomcat启动分析"文章读后笔记 jdk5.0 范型说明 java 数组反射例子 HTML小技巧 ResourceBundle 与locale的使用 jni中关于dll的装载问题 java线程-消费者vs生产者 HttpSessionBindingListener实现与应用 用Java Servlets 2.4实现过滤 加和不加java:comp/env/前缀有什么区别?
MessageFormat初步
daniel-shen · 2006-11-29 · via 博客园 - daniel-shen

Posted on 2006-11-29 13:51  daniel-shen  阅读(1107)  评论()    收藏  举报

public class MessageFormat
extends Format

MessageFormat 提供了一种用中性语言方式产生连接信息的方法。 用该类构造显示给最终用户的信息。

MessageFormat 产生一系列对象,格式化它们,然后把格式化的字符串插入模式中恰当的地方。

注意: MessageFormat 与其它 Format 类不同,用对象的构造子创建一个 MessageFormat 对象 ( 而不是用一个 getInstance 风格的工厂方法 )。 不需要这些工厂方法,因为 MessageFormat 不要求为给定的语言环境进行复杂的设置。 实际上,MessageFormat 根本没有实现任何语言环境特定的行为。 它只需要以句子为基础在句子上建立。

下面是一些用法示例:

 Object[] arguments = {
new Integer(7),
new Date(System.currentTimeMillis()),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
arguments);
: At 12:30 PM on Jul 3, 2053, there was a disturbance
in the Force on planet 7.
通常,消息的格式来自于资源,且参数将在运行期间动态的设置。

示例 2:

 Object[] testArgs = {new Long(3), "MyDisk"};
MessageFormat form = new MessageFormat(
"The disk \"{1}\" contains {0} file(s).");
System.out.println(form.format(testArgs));
// output, with different testArgs
: The disk "MyDisk" contains 0 file(s).
: The disk "MyDisk" contains 1 file(s).
: The disk "MyDisk" contains 1,273 file(s).

模式的形式是:

 messageFormatPattern := string ( "{" messageFormatElement "}" string )*
messageFormatElement := argument { "," elementFormat }
elementFormat := "time" { "," datetimeStyle }
| "date" { "," datetimeStyle }
| "number" { "," numberStyle }
| "choice" { "," choiceStyle }
datetimeStyle := "short"
| "medium"
| "long"
| "full"
| dateFormatPattern
numberStyle := "currency"
| "percent"
| "integer"
| numberFormatPattern
choiceStyle := choiceFormatPattern
如果没有 elementFormat,那么参数必须用一个字符串代替。 如果没有 dateTimeStylenumberStyle,就使用缺省格式 ( 例如,NumberFormat.getInstanceDateFormat.getTimeInstanceDateFormat.getInstance)。

必要的话,在字符串中的单引号可用来引起 "{"( 花括号 )。 一个真正的单引号用 '' 表示。 在 messageFormatElement 中,引号被删除。 例如,{1,number,$'#',##} 将产生一个含有被引起的磅符号的数字格式,结果是诸如 "$#31,45" 的形式。

如果使用了一个模式,模式中如果有未括起的花括号,它们必须匹配:即 "ab {0} de" 和 "ab '}' de" 是合法的,但是 "ab {0'}' de" and "ab } de" 是非法的。

参数是 0 到 9 之间的数字,它相应与格式化的数组中的参数。

数组中可以含有未用的参数。 但如果缺少参数或参数不是特定格式要求的正确的类的对象,将抛出一个 ParseException。 首先, format 检验是否用 setFormats 方法为参数指定了一个 Format 对象。 如果指定了,那么 format 使用这个 Format 对象格式化该参数。 否则根据对象的类型格式化参数。 如果参数是一个 Number,那么 formatNumberFormat.getInstance 格式化该参数;如果参数是一个 Date,那么 formatDateFormat.getDateTimeInstance 格式化该参数。 否则它使用 toString 方法。

对于更复杂的模式,可用 ChoiceFormat 输出,如:

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
double[] filelimits = {0,1,2};
String[] filepart = {"no files","one file","{0,number} files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
form.setFormat(1,fileform); // NOT zero, see below
Object[] testArgs = {new Long(12373), "MyDisk"};
System.out.println(form.format(testArgs));
// output, with different testArgs
output: The disk "MyDisk" contains no files.
output: The disk "MyDisk" contains one file.
output: The disk "MyDisk" contains 1,273 files.
可以象上面示例中那样用程序完成格式化,也可以用一个模式 ( 有关的更多信息请参阅 ChoiceFormat) ,如:
 form.applyPattern(
"There {0,choice,0#are no files|1#is one file|1#are {0,number,integer} files}.");

注意: 就象上面看到的那样,由 MessageFormat 中的 ChoiceFormat 产生的字符串将被特别对待;'{' 用于指示子格式并将产生递归。 如果用程序创建了 MessageFormatChoiceFormat ( 而不是用字符串模式 ),那么注意不要产生一个在自身上递归的格式,这将引起无限循环。

注意: 格式按照字符串中变量的顺序编号。这与参数编号不同。 例如:"abc{2}def{3}ghi{0}...",

  • format0 影响第一个变量 {2}
  • format1 影响第二个变量 {3}
  • format2 影响第二个变量 {0}
  • 依次类推。

可以在 applyPattern 之后使用 setLocale ( 可能再调用 setFormat) 以不同的语言环境重新初始化 MessageFormat