There is a ticket in lighthouse https://rails.lighthouseapp.com/projects/8994/tickets/2409-activerecord-double-escapes-error_messages_for for this problem, and the resolution is to append _html
to the i18n key in the locales/xx.yml
file and use the t
alias1 to denote an html_safe string. For example:
en:
hello: "This is a string with an accent: ó"
becomes:
en:
hello_html: "This is a string with an accent: ó"
它将创建以下输出:
这是一个带有重音符号的字符串:ó
这将阻止你写raw t('views.signup.organisation_details')
并会产生更干净的输出:t('views.signup.organisation_details_html')
。并且在交换的同时raw
for _html
看起来并不是最伟大的交易,它确实清楚地表明您正在输出假定为 html_safe 字符串的内容。
1 I've tested the code suggested in the lighthouse ticket. What I found was that you had to specifically use the
t
alias. If you used
I18n.t
or
I18n.translate
the translation didn't treat
_html
as html_safe:
I18n.t('hello_html')
I18n.translate('hello_html')
# Produces => "This is a string with an accent: ó"
t('hello_html')
# Produces => "This is a string with an accent: ó"
我认为这不是预期的行为RoR TranslationHelper 文档 http://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-translate.