如何在messages.properties文件中使用参数?

时间:2013-03-24 22:55:56

标签: java

this question中,它指出,可能有类似的东西:

message.myMessage = This message is for {0} in {1}

但我不知道如何将参数传递给它

MESSAGES.getString("message.myMessage", "foor", "bar")

但遗憾的是getString无法知道其他参数 有什么想法吗?

2 个答案:

答案 0 :(得分:28)

我猜你在考虑MessageFormat?如果是这样,就是这样:

String s = MessageFormat.format("This message is for {0} in {1}", "foo", "bar");

或者来自物业:

Properties p = new Properties();
p.setProperty("messages.myMessage", "This message is for {0} in {1}");
String s = MessageFormat.format(
    p.getProperty("messages.myMessage"), "foo", "bar");

答案 1 :(得分:11)

试试这个:

String message = "This message is for {0} in {1}.";
String result = MessageFormat.format(message, "me", "the next morning");
System.out.println(result);

java.text.MessageFormat;

或者在JSF中:

<h:outputFormat value="This message is for {0} in {1}.">
    <f:param value="me">
    <f:param value="the next morning">
</h:outputFormat>