Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use one Message as argument in other Spring Messages (properties file)

I have a need to do something like this:

bob.common=goat
bob.have=I have a {bob.common}!
bob.want=I want a {bob.common}!
bob.need=I need a {bob.common}!

Is this sort of thing possible? I know this seems silly, but being able to re-use a common piece is a need here, and we really can't (don't want to) do it programmatically.

We're already using numbered arguments in our properties, but we would like to be able to pass in a reference to another property.

like image 586
Andy Avatar asked Nov 01 '11 21:11

Andy


2 Answers

I suggest to do this :

bob.common=goat 
bob.have=I have a {0}!
bob.want=I want a {0}!
bob.need=I need a {0}!

Then in your page :

<spring:message code="bob.common" var="animal"/>
<spring:message code="bob.have" arguments="${animal}"/>
<spring:message code="bob.want" arguments="${animal}"/>
<spring:message code="bob.need" arguments="${animal}"/>

The way you want to do would be too strict, if you want to change your animal for example.

like image 109
Julien Feniou Avatar answered Nov 16 '22 11:11

Julien Feniou


According to the Spring changelog, this has been supported since 2.5.3:

  • PropertyPlaceholderConfigurer supports nested keys in placeholder keys as well (e.g. "${db.${environment}}")

So for your example case, you should be able to use:

bob.have=I have a ${bob.common}!

and the PropertyPlaceholderConfigurer should recognise the "nested key" and resolve that correctly.

like image 40
millhouse Avatar answered Nov 16 '22 11:11

millhouse