はじめに
Railsアプリのi18n(tメソッド)をJSでも使用できるようにしています。
CSSの擬似要素(content)を多言語化したかったのですが、style内でjavascriptは使えないのでどのように実装するか悩みました。
解決策
JQueryのappendで擬似要素を追加しました。
test.html
<div class="myclass">質問</div>
test.js
const required = I18n.t('helpers.required')
$('head').append(`<style>.myclass:before { content: "${required}" }</style>`);