i18n HTML 转义无效
i18n HTML escaping not working
根据 Rails 指南 (http://guides.rubyonrails.org/i18n.html#using-safe-html-translations) 上的 Ruby,我需要做的就是在不调用 html_safe
的情况下呈现我的翻译是有键名以 _html
结尾。这是我试过的:
en:
breadcrumbs:
root_html: "<i class='material-icons'>home</i>"
这样称呼它:
I18n.t('breadcrumbs.root_html')
导致输出是在我的翻译中定义的这个字符串,而不是呈现的 HTML。
我做错了什么?
在 Rails 上使用 Ruby 4.2.1。
提前致谢!
在指南中进一步向下滚动我发现了问题:
Automatic conversion to HTML safe translate text is only available from the translate view helper method.
因为我试图在我的控制器中准备 link 并通过 gem 将其传递给视图,所以这没有用。
要完成这项工作,您必须在字符串上调用 html_safe
,如下所示:
I18n.t('breadcrumbs.root_html').html_safe
如果您找到其他解决方案,请联系我!
根据 Rails 指南 (http://guides.rubyonrails.org/i18n.html#using-safe-html-translations) 上的 Ruby,我需要做的就是在不调用 html_safe
的情况下呈现我的翻译是有键名以 _html
结尾。这是我试过的:
en:
breadcrumbs:
root_html: "<i class='material-icons'>home</i>"
这样称呼它:
I18n.t('breadcrumbs.root_html')
导致输出是在我的翻译中定义的这个字符串,而不是呈现的 HTML。 我做错了什么?
在 Rails 上使用 Ruby 4.2.1。 提前致谢!
在指南中进一步向下滚动我发现了问题:
Automatic conversion to HTML safe translate text is only available from the translate view helper method.
因为我试图在我的控制器中准备 link 并通过 gem 将其传递给视图,所以这没有用。
要完成这项工作,您必须在字符串上调用 html_safe
,如下所示:
I18n.t('breadcrumbs.root_html').html_safe
如果您找到其他解决方案,请联系我!