Last Updated: September 09, 2019
·
7.559K
· marcosx

Unescaped html inside yml lang file in rails

During the upgrade from Rails 2.x to Rails 3.x, we faced some issues because on rails 3.x every value inside the yaml lang file (for example en.yml) is escaped by default, and we had tons of entries with html code.

After looking for solutions (like calling #raw) we found on this stack overflow topic (http://stackoverflow.com/questions/3626038/use-html-inside-a-rails-translation-file) that we can just add a "_html" to the end of the value and Rails won't do anything with it.

It was a better solution to us because we had a lot of places calling the translate method and we could keep all the changes inside the yaml files, instead of changing tons of views. It was also good because people could still use the translation method in the way they are used to.

Using the lang file that rails provides in its template app we could do something like that:

en:
  hello: <b>Hello world</b>
  hello_html: <b>Hello World</b>
<%= t('hello')  %>
<br/>
<%= t('hello_html')  %>

This code will produce this:

<b>Hello world</b> 
Hello World

Hope this can help someone out there!