为什么在某些语言环境中没有正确填充Spring MessageSource参数?

Rih*_*rds 29 spring arguments internationalization double-quotes

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>
Run Code Online (Sandbox Code Playgroud)

以上是用于下面的代码的特定消息.

String country = "AU";
Object[] args = new Object[] { account.getLogin(), confirm.getHash() };

helper.setText(appContext.getMessage("mailconfirm.mail.body", args,
                new Locale(country)), true);
Run Code Online (Sandbox Code Playgroud)

我调试了两个参数,它们都有正确的值.在调试appContext.getMessage线时,我看到{1}param没有填充正确的值,但是{0}.

什么想法可能是错的?我怀疑这可能是某些语言环境问题.

Rih*_*rds 63

问题解决了!看来问题是因为邮件mailconfirm.mail.body在{0}之后和{1}之间的某处包含了一个撇号.doesn'tdoes not它替换后修复了问题.我不知道撇号不能在那里使用.PS这是一个错误还是我的错误和撇号应该被逃脱?

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>
Run Code Online (Sandbox Code Playgroud)

一个人doesn't花了我一个小时来弄明白并推动修复.哈哈哈..从现在开始,我认为撇号是邪恶的!

  • 万岁!因为你花了一个小时,我没有必要.而且,事实证明,你可以通过将它们加倍来使你的单引号正常工作,比如'''`.这将呈现为单引号,并且它也将与MessageSource参数很好地协作.但令人困惑的是,此**仅适用于具有参数的属性字符串,否则将导致显示两个撇号. (34认同)
  • 此链接可能有助于更好地了解正在发生的事情:http://blog.pfa-labs.com/2010/07/infamous-dissapearing-single-quote-in.html (3认同)

mic*_*cha 33

Spring ResourceBundleMessageSource(我认为你正在使用)MessageFormat用于替换{0}消息中的占位符().MessageFormat要求'使用两个单引号('')转义单引号()(请参阅:MessageFormat Javadoc).

但是,默认情况下,不会解析不包含任何参数的消息MessageFormat.所以不带参数的消息中的单引号不需要转义.

ResourceBundleMessageSource提供了一个标志alwaysUseMessageFormat,如果MessageFormat应该应用于所有消息,则可以使用该标志.因此,单引号总是需要通过两个单引号进行转义.

有关详细信息,请参阅此博客文章.


Mal*_*lai 7

我无法说服我的业务团队在所需的地方添加双人,有时他们也会忘记.所以我只是覆盖ReloadableResourceBundleMessageSource#loadProperties了:如果值包含,"'"&"{0",则替换"'"with "''"并使用相同的键放入属性.