【问题标题】:How to use HTML in grails messages.properties for mail sending如何在 grails messages.properties 中使用 HTML 进行邮件发送
【发布时间】:2011-03-11 02:18:47
【问题描述】:

在 grails 中,我使用 GSP 模板来呈现使用邮件插件发送的 HTML 电子邮件。这很好用,但是 GSP 模板使用了一个参数,该参数又从我的 messages.properties 文件中检索到。现在我想使用 HTML,例如<br/> 在messages.properties 中,但它在邮件中显示为文本并且标签不会被解释。

我已经尝试在 GSP 中的参数上使用 .decodeHTML(),但它不起作用。

我必须在哪里编码/解码,或者是否可以在 messages.properties 中使用 HTML 标记?

<%@ page contentType="text/html"%>
<html>
<body>
${variableWithHTMLTextFromMessagesProperties}
</body>
</html>

【问题讨论】:

  • 你能发布普惠制吗?您是否在 GSP 模板的消息标签中使用了 encodeAs 属性?

标签: html grails internationalization view decode


【解决方案1】:

可以不使用message标签在GSP中进行本地化,类似于下面这样吗?控制器 -

sendMail {
    to "my@email.com"
    subject "cheese"
    html g.render(template:"myTemplate")
}

然后在你的 _myTemplate.gsp -

<%@ page contentType="text/html" %>
<html><head></head>
<body>
    <p>test: <g:message code="a.test"/></p>
</body>
</html>

然后在messages.properties -

a.test=this <br/><br/> is a test

这种方式对我来说很好(Grails 1.3.1,邮件 0.9),我收到的 html 电子邮件中有 2 个换行符。你有什么理由不能这样做?

【讨论】:

  • 实际上,在我的情况下,messages.properties 中的文本预先保存为持久类中的字符串,然后将其传递给视图。当然我理论上可以存储消息代码,但是我还需要存储以后应该使用的参数,所以我宁愿直接存储消息......但我想我会如果它不能以任何其他方式工作,请将您的解决方案用作备用解决方案,所以谢谢!
【解决方案2】:

找到解决方案here。最简单的方法就是使用&lt;%=variableWithHTMLTextFromMessagesProperties%&gt; 而不是${variableWithHTMLTextFromMessagesProperties}。这会阻止 HTML 转义。

【讨论】:

    【解决方案3】:

    我使用自定义 taglib 制作了自己的解决方案。

    def htmlMessage = { attrs, body ->
        out << g.message(attrs, body).decodeHTML()
    }
    

    然后要定义一个消息,它必须带有转义的html:

    my.html.message={0,choice,0#There is no elements|1#There is &lt;strong&gt;1&lt;/strong&gt; element|1<There are &lt;strong&gt;{0}&lt;/strong&gt; elements}
    

    仅适用于 html:

    <g:htmlMessage code="my.html.message" args="[qElements]" />
    

    结果是一个 i18n html 生成的带有强字体的数字。像这样:

    “有 9 个元素”

    【讨论】:

      【解决方案4】:

      来自docs

      <g:encodeAs codec="HTML">
         Profile for user ${user.name} is:
         <g:render template="/fragments/showprofile"/>
      </g:encodeAs>
      

      【讨论】:

      • 这是错误的方向;现在messages.properties中的
        显示为<br/>
      猜你喜欢
      • 1970-01-01
      • 2010-11-24
      • 2012-11-23
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2021-01-25
      相关资源
      最近更新 更多