Spring message tag has troubles with the single quote character
Asked Answered
D

1

6

I have 3 properties defined in my resource bundle:

test.key1 = Just a normal snippet.
test.key2 = snippet with {0} arguments {1}.
test.key3 = snippet with 'charac'ter'.
test.key4 = snippet with {0} arguments' and 'characters {1}.

Here's how I access them in my jsp:

<spring:message code="test.key1"/>
<spring:message code="test.key2" arguments="ARG1,ARG2"/>
<spring:message code="test.key3"/>
<spring:message code="test.key4" arguments="ARG1,ARG2"/>

Rendered they look like this:

Just a normal snippet.
snippet with ARG1 arguments ARG2.
snippet with 'charac'ter'.
snippet with ARG1 arguments and characters {1}.

So basically what happens is: As soon as a ' (single quote) character appears, the arguments are no longer filled in AND the single quotes are not rendered anymore. The funny thing is spring has no problems when arguments or single quotes are applied separately.

I was able to fix test scenario 4 by using 2x single quotes like this:

test.key4 = snippet with {0} arguments'' and ''characters {1}.

Which correctly renders too

snippet with ARG1 arguments' and 'characters ARG2.

Of course this a maintenance nightmare: all properties that have single quotes in them but NO arguments should have 1x the single quotes, and all properties that have both single quote characters and arguments in them require 2x single quote.

Why is this, and is there a way to fix this undesired behaviour?

Dihedral answered 1/6, 2015 at 17:8 Comment(0)
D
6

Apparently, this weird behaviour is "as designed". all information is summarized in this article.

To fix this, you either double your single quotes only in properties that have arguments, or you add an additional property to your message reosurce bean, in which case ALL single quotes have to doubled, even in properties that don't have arguments. This is much more consistent!

<property name="alwaysUseMessageFormat" value="true"/>
Dihedral answered 2/6, 2015 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.