根据 Ruby on Rails 指南 ( http://guides.rubyonrails.org/i18n.html#using-safe-html-translations ),我需要做的就是在不调用 html_safe
的情况下呈现我的翻译。在它们上面是让键名以 _html
结尾.这是我尝试过的:
en:
breadcrumbs:
root_html: "<i class='material-icons'>home</i>"
像这样调用它:
I18n.t('breadcrumbs.root_html')
导致输出是在我的翻译中定义的这个字符串,而不是呈现的 HTML。
我究竟做错了什么?
使用 Ruby on Rails 4.2.1。
提前致谢!
最佳答案
在指南中向下滚动一点,我发现了问题:
Automatic conversion to HTML safe translate text is only available from the translate view helper method.
由于我尝试在我的 Controller 中准备链接并将其通过 gem 传递到 View ,因此这不起作用。
要完成这项工作,您必须调用
html_safe
在字符串上,像这样:I18n.t('breadcrumbs.root_html').html_safe
如果您找到其他解决方案,请联系我!
关于ruby-on-rails-4 - i18n HTML 转义不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773952/