Why Spring MessageSource arguments are not filled correctly in some locales?

31,753

Solution 1

Issue solved! It appears that the problem was because the message mailconfirm.mail.body contained an apostrophe somewhere after {0} and between {1}. After replaced doesn't with does not it fixed the problem. I didn't know apostrophes can't be used in there. P.S. Is it a bug or just my mistake and apostrophes should be escaped?

mailconfirm.mail.body=<html><body><h3 style="margin: 0 0 1em;">Hi, {0}!</h3>\
    To confirm your email address, click on the confirmation link given bellow. If clicking on the link doesn't work, copy and paste the link in a new browser tab. <br /><br />\
    <a href="http://www.domain.com/confirm_email.html?action=activate&hash={1}">http://www.domain.com/confirm_email.html?action=activate&hash={1}</a><br /><br />\
    Kind regards,<br />\
    Your Something
    </body></html>

One doesn't took me about an hour to figure it out and push a fix. Hahaha.. From now on I consider apostrophes being evil!

Solution 2

Spring's ResourceBundleMessageSource (which I think you are using) uses MessageFormat for replacing placeholders ({0}) inside messages. MessageFormat requires that single quotes (') are escaped using two single quotes ('') (see: MessageFormat Javadoc).

However, by default messages that do not contain any arguments will not be parsed by MessageFormat. So single quotes in messages without arguments don't need to be escaped.

ResourceBundleMessageSource provides a flag called alwaysUseMessageFormat that can be used if MessageFormat should be applied to all messages. So a single quote need always be escaped by two single quotes.

See this blog post for more details.

Solution 3

I am not able to convince my business team to add double apos in required places and some times they are forgetting also. So I just overrided ReloadableResourceBundleMessageSource#loadProperties as : if the value contains, "'" & "{0", then replace the "'" with "''" and put in to the Properties with the same key.

Share:
31,753
Rihards
Author by

Rihards

Updated on December 29, 2020

Comments

  • Rihards
    Rihards over 3 years
    mailconfirm.mail.body=<html><body><h3 style="margin: 0 0 1em;">Hi, {0}!</h3>\
        To confirm your email address click on the confirmation link given bellow. If clicking on the link doesn't work, copy and paste the link in a new browser tab. <br /><br />\
        <a href="http://www.domain.com/confirm_email.html?action=activate&hash={1}">http://www.domain.com/confirm_email.html?action=activate&hash={1}</a><br /><br />\
        Kind regards,<br />\
        Your Something
        </body></html>
    

    This above is the particular message used for the code bellow.

    String country = "AU";
    Object[] args = new Object[] { account.getLogin(), confirm.getHash() };
    
    helper.setText(appContext.getMessage("mailconfirm.mail.body", args,
                    new Locale(country)), true);
    

    I debugged both arguments and they both have the right values. When debuging appContext.getMessage line, I saw that the {1} param is not filled with correct value however {0} is.

    Any ideas what could be wrong? I suspect it could be some locale issue.

  • aroth
    aroth almost 13 years
    Hooray! Because you spent an hour on this I didn't have to. Also, it turns out that you can get your single quotes to work properly by doubling them, like ''. This will render as a single quote, and it will also play nicely with MessageSource arguments. But confusingly, this only works in property strings that have parameters, otherwise it will result in both apostrophes being displayed.
  • Mopper
    Mopper over 11 years
    This link may be useful to better understand what is going on: blog.pfa-labs.com/2010/07/…
  • Aron
    Aron over 9 years
    Yes, it is important to add double ' , ' in your messages.properties. Example: user.locked = Votre compte a \u00E9t\u00E9 bloqu\u00E9 parce que vous avez saisis {0} fois de suite un mauvais mot de passe. Ce blocage s''effectue pendant {1} minutes.
  • EpicPandaForce
    EpicPandaForce over 9 years
    same for mscharhag.com/2013/10/… and the ResourceBundleMessageSource's alwaysUseMessageFormat flag
  • Selwyn
    Selwyn about 9 years
    note: if you're going to copy/paste the above mailconfirm.mail.body..... be sure to change the doesn't -> does not.... I spent 30 min ironically trouble shooting this haha.
  • Vanderson Assis
    Vanderson Assis over 4 years
    Saved me hours and more hours of researching and failing miserably...THANK YOU!!!!