How can I used named parameters in a messages.properties file? How can I used named parameters in a messages.properties file? spring spring

How can I used named parameters in a messages.properties file?


After facing the very same question and poking in source code I found a "loop-hole" that makes it possible in a very easy way:

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

This approach doesn't eliminate usage of numbers. The reason to use it is to give hints to translation folks.


I am afraid not, parameters are an Object array so there is no way to define names for them. If you always passes in the array of parameter in the same order though you could use them like this:

message.myMessage = This message is for {0} in {1}message.myNameMessage = This message is for {0}message.myLocationMessage = This message is for people in {1}message.myAlternateMessage = The message params are location: {1}; name: {0}


Take a look at ICU4J

It allows for something like this:

message.myMessage=This message is for {name} in {location}.

And it is way more powerful than the simple replacements suggested, because can do locale aware formatting of the parameters (ie: "Subscription expires on: {expirationDate, date, long})

http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html