html escape with rails I18n

railsI18nruby
tomoyukikashiro
tomoyukikashiro

String is always html escaped in ‘config/locales/en.yml’

‘config/locales/en.yml’

en:
  hello_message: "Hello <strong>%{user_name}</strong>"

hello.html.erb

<%= t("hello_message", user_name: @user.name) %>

check hello.html in your brower.
<strong></strong>tag is html escaped automatically.

hello <strong>Tomoyuki</strong>

html source is like this

hello &lt;strong&gt;Tomoyuki&lt;/strong&gt;

Output html and locales with html safe

You can set locale and set html tags in ‘conf/locales/XX.yml’ with html safe.
Try to use ‘XXX_html’ suffix in your locale key.

en:
  hello_message_html: "Hello <strong>%{user_name}</strong>"

hello.html.erb

<%= t("hello_message", user_name: @user.name) %>

check hello.html in your brower.
<strong></strong>tag is not html escaped automatically.

hello Tomoyuki

html source is like this

hello <strong>Tomoyuki</strong>

Reference