繁体   English   中英

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

[英]How can I use parameters in a messages.properties file?

这个问题中,它指出,可能有类似的东西:

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

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

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

但不幸的是 getString 不知道带其他参数有什么想法吗?

我猜你在想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");

试试这个:

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>

您可以使用字符串数组。

String[] params = new String[2]; 
params[0] = "This is your text including parameters";
params[1] = "This is your second text including parameters";

如果您使用的是 JSF,请以这种方式从您的支持 bean 将 Array 作为警告文本传递给文本:

JsfUtil.addWarn("A_MSG_TITLE_IN_PROPERTIES_FILE", params);

并将这一行插入到您的本地属性文件中:

A_MSG_TITLE_IN_PROPERTIES_FILE = Hello {0} and {1} 

所以,输出将是:

您好,这是您的包含参数的文本,这是您的第二个包含参数的文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM