railsのI18nでhtmlタグを埋め込む方法

# config/locales/en.yml
en:
welcome: <b>welcome!</b>
hello_html: <b>hello!</b>
title:
html: <b>title!</b>
# app/views/home/index.html.erb
<div><%= t('welcome') %></div>
<div><%= raw t('welcome') %></div>
<div><%= t('hello_html') %></div>
<div><%= t('title.html') %></div>

t(‘hoge’)のようなtranslateビューヘルパーメソッドではお尻に_htmlをつければ安全にhtmlタグを表示してくれます。

I18n.tのような呼び出し方の場合は上記の呼び出しかたができず、rawを使うしかないです。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です