I am trying to get i18n message like below:
messageCode=Test message for {0} and {1} and {2}.
In jsp, I have this:
<spring:message code="messageCode"
arguments="${value1},${value2},${value3}"
htmlEscape="false"/>
The arguments:
value1=A,B
value2=C,D
value3=E,F
The output for what I want:
Test message for A,B and C,D and E,F
The actual output:
Test message for A and B and C
Is there any way to overcome this? Thank you.
George
The cause of the probelm is that ,
(comma) is the default separator. So at the end the spring message tag will get the String A,B,C,D,E,F
for parameter arguments
, and it will split this string into 6 different internal arguments for the message.
You must change the separator. If you use ;
for example, then it will work.
<spring:message code="messageCode"
arguments="${value1};${value2};${value3}"
htmlEscape="false"
argumentSeparator=";"/>
@See Spring Reference: Appendix F.6 The Message Tag
You could also send the different values as an array and leave no room for spring making a mistake in how to parse the string argument.
<c:set var="value1" value="A,B;X" />
<c:set var="value2" value="C,D;Y" />
<c:set var="value3" value="E,F;Z" />
<spring:message code="messageCode"
arguments="${[value1, value2, value3]}"
htmlEscape="false" />
This way you need no worry about your new separator somehow being used in a value at some point again.
I use completely different approach. My database-based message source is exposed in my config with the name i18n:
@Bean(name = {"i18n", "messageSource"})
public MessageSource messageSource() {
return new JpaMessageSource();
}
and I also expose my beans with viewResolver.setExposeContextBeansAsAttributes(true);
After that I can use ${i18n.message("messageCode", value1, value2, value3)}
in my jsp-views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With