[英]How can I use parameters in a messages.properties file?
In this question , it points out, It's possible to have something like:在这个问题中,它指出,可能有类似的东西:
message.myMessage = This message is for {0} in {1}
But I don't know how to pass parameter to it但我不知道如何将参数传递给它
MESSAGES.getString("message.myMessage", "foor", "bar")
but unfortunately getString can't know take other parameters Any idea?但不幸的是 getString 不知道带其他参数有什么想法吗?
I'm guessing you're thinking of MessageFormat ? 我猜你在想MessageFormat ? If so, it's just this:
如果是这样,就是这样:
String s = MessageFormat.format("This message is for {0} in {1}", "foo", "bar");
Or from properties: 或者从属性:
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");
Try out this one: 试试这个:
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;
) (
java.text.MessageFormat;
)
Or in JSF: 或者在JSF中:
<h:outputFormat value="This message is for {0} in {1}.">
<f:param value="me">
<f:param value="the next morning">
</h:outputFormat>
You can use a String Array.您可以使用字符串数组。
String[] params = new String[2];
params[0] = "This is your text including parameters";
params[1] = "This is your second text including parameters";
If you are using JSF, pass Array to the text this way from your backing bean as a warning text :如果您使用的是 JSF,请以这种方式从您的支持 bean 将 Array 作为警告文本传递给文本:
JsfUtil.addWarn("A_MSG_TITLE_IN_PROPERTIES_FILE", params);
And insert this line to your local properties file:并将这一行插入到您的本地属性文件中:
A_MSG_TITLE_IN_PROPERTIES_FILE = Hello {0} and {1}
So, output will be :所以,输出将是:
Hello This is your text including parameters and This is your second text including parameters您好,这是您的包含参数的文本,这是您的第二个包含参数的文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.