Use HTML inside a Rails translation file

Fu86 picture Fu86 · Sep 2, 2010 · Viewed 18.2k times · Source

I have some translations in my Rails application (config/locale/[en|de].yml) and I use it in my views with <%=t "teasers.welcome" %>. Example:

teasers:
    welcome: "<strong>Welcome</strong> to the Website ..."

In Rails 2.3.8 this works just fine, with Rails 3, the HTML is escaped and translated to &lt;... How can I prevent this form this translation and use HTML in my translation files like in Rails 2.3.8?

Answer

Simone Carletti picture Simone Carletti · Sep 2, 2010

Other than using raw, there's an other undocumented (but official) way to do so. All keys ending with _html are automatically rendered unescaped.

Rename the key from

teasers:
    welcome: "<strong>Welcome</strong> to the Website ..."

to

teasers:
    welcome_html: "<strong>Welcome</strong> to the Website ..."