为什么在某些语言环境中没有正确填写 Spring MessageSource 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6332378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Spring MessageSource arguments are not filled correctly in some locales?
提问by Rihards
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.getMessageline, I saw that the {1}param is not filled with correct value however {0}is.
我调试了两个参数,它们都有正确的值。在调试appContext.getMessage线路时,我看到{1}参数没有填充正确的值{0}。
Any ideas what could be wrong? I suspect it could be some locale issue.
任何想法可能是错误的?我怀疑这可能是一些语言环境问题。
回答by Rihards
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'twith does notit 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 在 {0} 之后和 {1} 之间的某处包含撇号。更换完毕后doesn't用does 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>
One doesn'ttook me about an hour to figure it out and push a fix. Hahaha.. From now on I consider apostrophes being evil!
一个doesn't我花了大约一个小时来弄明白,推动修复。哈哈哈..从现在开始我认为撇号是邪恶的!
回答by micha
Spring's ResourceBundleMessageSource(which I think you are using) uses MessageFormatfor replacing placeholders ({0}) inside messages. MessageFormatrequires that single quotes (') are escaped using two single quotes ('') (see: MessageFormat Javadoc).
Spring 的ResourceBundleMessageSource(我认为您正在使用)MessageFormat用于替换{0}消息中的占位符 ( )。MessageFormat要求'使用两个单引号 ( '')对单引号 ( ) 进行转义(请参阅: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.
但是,默认情况下,不包含任何参数的消息不会被MessageFormat. 因此不需要转义没有参数的消息中的单引号。
ResourceBundleMessageSourceprovides a flag called alwaysUseMessageFormatthat can be used if MessageFormatshould be applied to all messages. So a single quote need always be escaped by two single quotes.
ResourceBundleMessageSource提供了一个名为的标志alwaysUseMessageFormat,如果MessageFormat应将其应用于所有消息,则可以使用该标志。所以一个单引号总是需要被两个单引号转义。
See this blog postfor more details.
有关更多详细信息,请参阅此博客文章。
回答by Malai
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#loadPropertiesas :
if the value contains, "'"& "{0", then replace the "'"with "''"and put in to the Properties with the same key.
我无法说服我的业务团队在需要的地方添加双引号,有时他们也会忘记。所以我只是重写ReloadableResourceBundleMessageSource#loadProperties为:如果该值包含"'"& "{0",则替换"'"with"''"并使用相同的键放入 Properties 中。
回答by Willem Kop
An alternative is to use String.format, change the {X}for %s.
另一种方法是使用String.format,更改{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.
希望它有用。

