Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 346
mko Avatar asked Mar 24 '13 22:03

mko


People also ask

How do I get environment variables from properties file?

The System class in Java provides a method named System. getenv() which can be used to get the value of an environment variable set in the current system.


2 Answers

I'm guessing you're thinking of 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"); 
like image 158
pmorken Avatar answered Oct 20 '22 01:10

pmorken


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;)

Or in JSF:

<h:outputFormat value="This message is for {0} in {1}.">     <f:param value="me">     <f:param value="the next morning"> </h:outputFormat> 
like image 43
vhunsicker Avatar answered Oct 20 '22 01:10

vhunsicker