JSTL - apostrophe in message bundles

Published: Sunday, 9 April 2006

When requesting translations you must remember that an apostrophe must be escaped when using messages with arguments.

Consider the contents of the following properties file

test.apos1=one ' apos
test.apos2=double '' apos
test.apos1.arg=one ' apos and arg is {0}
test.apos2.arg=two '' apos and arg is {0}

in conjuction with the following JSP snippet

<pre>
    test.apos1: <fmt:message key="test.apos1"/>
    test.apos2: <fmt:message key="test.apos2"/>
    test.apos1.arg: <fmt:message key="test.apos1.arg">
        <fmt:param>hello</fmt:param>
    </fmt:message>
    test.apos2.arg: <fmt:message key="test.apos2.arg">
        <fmt:param>hello</fmt:param>
    </fmt:message>
</pre>

produces

    test.apos1: one ' apos
    test.apos2: double '' apos
    test.apos1.arg: one  apos and arg is {0}
    test.apos2.arg: two ' apos and arg is hello

The only unusual case is the 3rd line, where we have used one apostrophe with one argument. To work around this escape the apostrophe with another apostrophe, as with the 4th example, whenever you use arguments.