Use HTML inside a Rails translation file Use HTML inside a Rails translation file ruby-on-rails ruby-on-rails

Use HTML inside a Rails translation file


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 ..."


I suppose it's because doing

<%= t("blah") %>

in rails 2.x, now is the equivalent of doing

<%=h t("blah") %>

when you're using rails 3.

From the release notes:

Switch to on-by-default XSS escaping for rails.

To fix this, and once again from the release notes:

You no longer need to call h(string) to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call raw(string).

So just replace

<%= t("blah") %>

by

<%= raw t("blah") %>