为什么在某些语言环境中无法正确填充Spring MessageSource自变量?

|
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>
以上是用于以下代码的特定消息。
String country = \"AU\";
Object[] args = new Object[] { account.getLogin(), confirm.getHash() };

helper.setText(appContext.getMessage(\"mailconfirm.mail.body\", args,
                new Locale(country)), true);
我调试了两个参数,它们都具有正确的值。调试
appContext.getMessage
行时,我看到
{1}
参数没有填充正确的值,而
{0}
是。 任何想法可能有什么问题吗?我怀疑这可能是一些地区问题。     
已邀请:
        问题已解决! 看来问题出在此是因为邮件mailconfirm.mail.body在{0}之后到{1}之间包含撇号。将
doesn\'t
换成
does not
后,它解决了问题。我不知道在其中不能使用撇号。 附言是一个错误还是仅仅是我的错误而撇号应该被转义?
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>
一位5英镑的人花了我大约一个小时才弄清楚并修复问题。哈哈哈..从现在开始,我认为撇号是邪恶的!     
        Spring的ѭ9(我想您正在使用)使用ѭ10来替换消息中的占位符(ѭ4)。
MessageFormat
要求使用两个单引号(
\'\'
)对单引号(
\'
)进行转义(请参阅:MessageFormat Javadoc)。 但是,默认情况下,不包含任何参数的消息将不被ѭ10解析。因此,消息中不带参数的单引号不需要转义。
ResourceBundleMessageSource
提供一个称为
alwaysUseMessageFormat
的标志,如果应将all10ѭ应用于所有消息,则可以使用该标志。因此,单引号始终需要被两个单引号转义。 有关更多详细信息,请参见此博客文章。     
        我无法说服我的业务团队在所需位置添加双引号,有时他们也忘记了。 所以我只是将
ReloadableResourceBundleMessageSource#loadProperties
改写为: 如果值包含
\"\'\"
\"{0\"
,则将
\"\'\"
替换为
\"\'\'\"
,然后使用相同的键将其放入属性中。     
        替代方法是使用
String.format
,将
{X}
更改为
%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() );
希望它有用。     

要回复问题请先登录注册