开发者

i18n on Ruby on Rails, < and > gets replaced by &gt ; &lt ; when not intended

I am creating locale files for internationalization in a rails app, and have a url that I want translated with tags included , for example

html.erb

<%= t(foo.bar.xxxx) %>

yml file

foo: bar: xxxx: "xxxx"

result

&lt ;a href= "/info/index.html"&gt ;xxxx</a&gt ;

which bre开发者_开发技巧aks my links. I do not have an h on the ruby part, so shouldn't this work? Or should I just not have html tags within the yml file?

Rails version is 3.0.1 Ruby version is 1.8.7 p249


Your HTML YAML keys need to have a _html suffix:

foo:
  bar:
    xxxx_html: "<strong>Some HTML Here</strong>"

Doing this Rails will mark the string has html_safe and will render out the HTML instead of converting it to &gt; and &lt;.

You need to reference it with the full key name as well, Rails doesn't automatically see the _html suffix when you call xxxx.

<%= t 'foo.bar.xxxx_html' %>


Rails is preventing injection attacks by preventing model data from being displayed as actual markup. The raw function prevents that conversion.

Does

<%= raw t(foo.bar.xxxx) %> 

work?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜