【问题标题】:Why Spring MessageSource arguments are not filled correctly in some locales?为什么在某些语言环境中未正确填写 Spring MessageSource 参数?
【发布时间】:2011-09-14 00:18:56
【问题描述】:
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} 是。

有什么想法可能是错的吗?我怀疑这可能是一些语言环境问题。

【问题讨论】:

    标签: spring internationalization arguments double-quotes


    【解决方案1】:

    问题解决了! 问题似乎是因为邮件 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>
    

    一个doesn't 花了我大约一个小时来弄清楚并推动修复。哈哈哈..从现在开始我认为撇号是邪恶的!

    【讨论】:

    • 万岁!因为你花了一个小时在这我没必要。此外,事实证明,您可以通过将单引号加倍来使它们正常工作,例如''。这将呈现为单引号,并且它也可以很好地与 MessageSource 参数一起使用。但令人困惑的是,这个 only 适用于具有参数的属性字符串,否则会导致两个撇号都显示。
    • 此链接可能有助于更好地了解正在发生的事情:blog.pfa-labs.com/2010/07/…
    • mscharhag.com/2013/10/…ResourceBundleMessageSourcealwaysUseMessageFormat 标志相同
    • 注意:如果你要复制/粘贴上面的mailconfirm.mail.body..... 一定要改掉不->不....我花了30具有讽刺意味的是,解决这个问题哈哈。
    • 为我节省了数小时的研究和惨败...谢谢!!!!
    【解决方案2】:

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

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

    ResourceBundleMessageSource 提供了一个名为alwaysUseMessageFormat 的标志,如果应该将MessageFormat 应用于所有消息,则可以使用该标志。所以一个单引号总是需要被两个单引号转义。

    查看blog post了解更多详情。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 是的,在你的 messages.properties 中添加双 ' , ' 很重要。示例: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 吊坠 {1} 分钟。
      【解决方案4】:

      另一种方法是使用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() );
      

      希望有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-13
        • 2019-01-29
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        • 2013-07-01
        • 1970-01-01
        • 2018-02-17
        相关资源
        最近更新 更多