Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring message tag with multiple arguments

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

like image 596
George Sun Avatar asked Dec 21 '11 10:12

George Sun


3 Answers

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

like image 127
Ralph Avatar answered Oct 20 '22 19:10

Ralph


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.

like image 2
Mahdi Avatar answered Oct 20 '22 19:10

Mahdi


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.

like image 1
Vadim Ferderer Avatar answered Oct 20 '22 19:10

Vadim Ferderer