在Rails转换文件中使用HTML

时间:2010-09-02 10:05:49

标签: html ruby-on-rails localization translation

我的Rails应用程序(config / locale / [en | de] .yml)中有一些翻译,我在<%=t "teasers.welcome" %>的视图中使用它。例如:

teasers:
    welcome: "<strong>Welcome</strong> to the Website ..."

在Rails 2.3.8中这很好用,使用Rails 3,HTML被转义并转换为&lt; ...如何防止这种形式的翻译并在我的翻译文件中使用HTML,如在Rails中2.3.8?

2 个答案:

答案 0 :(得分:91)

除了使用raw之外,还有另一种无证(但是官方)的方法。 所有以_html结尾的键都会自动呈现为未转义状态。

重命名密钥
teasers:
    welcome: "<strong>Welcome</strong> to the Website ..."

teasers:
    welcome_html: "<strong>Welcome</strong> to the Website ..."

答案 1 :(得分:37)

我想这是因为做了

<%= t("blah") %>
在rails 2.x中的

,现在相当于做

<%=h t("blah") %>

当你使用rails 3时。

来自release note s:

  

切换到默认的XSS转义   对于铁路。

要解决此问题,请再次从发行说明中解决:

  

你不再需要调用h(字符串)   为了逃避HTML输出,它是通过   默认在所有视图模板中。如果你   想要未转义的字符串,请致电   原料(字符串)。

所以只需替换

<%= t("blah") %>

通过

<%= raw t("blah") %>