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

不想你离开。 提交于 2019-11-28 23:50:34

问题


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.


回答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!




回答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.




回答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.




回答4:


An alternative is to use String.format, change the {X} for %s.

mailconfirm.mail.body=<html><body><h3 style="margin: 0 0 1em;">Hi, %s!</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/confirm_email.html?action=activate&hash=%s">http://www.domain/confirm_email.html?action=activate&hash=%s</a><br /><br />\
    Kind regards,<br />\
    Your Something
    </body></html>


String whatEver = messageSource.getMessage("mailconfirm.mail.body", null, locale);

whatEver = String.format(whatEver,account.getLogin(), confirm.getHash() );

Hope it's useful.



来源:https://stackoverflow.com/questions/6332378/why-spring-messagesource-arguments-are-not-filled-correctly-in-some-locales

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!