Rails 3, i18n: Interpolating html tags into text

隐身守侯 提交于 2019-12-03 17:30:28

问题


My problem is exactly the one described in this question. Only difference: I'm using Rails 3 (3.0.4 exactly), which uses the %{...} syntax rather than the old {{...}}.

Problem: Rails 3 escapes all tags inside %{...} so I cannot interpolate, say, links in there.

Is there any way to overcome this?


回答1:


I finally figured it out, you have to use _html as a suffix for any yaml-keys, that you do not want html-escaped:

paragraph_html: "This is some text with a %{link}"
paragraph_linktext: "really cool link"

and then

link = link_to t( paragraph_linktext ), "http://www.example.com"
<%= t( paragraph_html, :link => link ) %>

and voila, there you have your i18n-ized link.




回答2:


You can do like this

my_translation: "Click to access %{link} page"

then:

<%= t( my_translation, :link => link_to("Google", "http://google.com") ).html_safe %>

And everything will be ok!



来源:https://stackoverflow.com/questions/5367705/rails-3-i18n-interpolating-html-tags-into-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!