簡體   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