html escape with rails I18n

June 05, 2015

Reading time ~1 minute

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

  • http://guides.rubyonrails.org/i18n.html#using-safe-html-translations

add ticket number to git commit automatically

Most of ticket tracker like Github, pivotal tracker have function to connect your commit to ticket(story).But every time when you commit ...… Continue reading

I will build frontend and backend separately

Published on September 28, 2016

order of angular controller's initialisation

Published on October 05, 2015