LoginSignup
0
0

More than 1 year has passed since last update.

【Rails】i18nをパーシャルに適用する

Posted at

はじめに

Ruby on Railsの学習を始めツイッタークローン作成時にi18nを用いた国際化を試みました。
i18nをパーシャルに適用する際の方法の備忘録になります。

i18nの導入やapplication.rbの修正等は実施してある前提です。

環境

・rails 5.0.7以上
・rails-18n 5.1以上

結論

viewと同じ記載で適用されます。
例えば、ルートに設定してるならこれだけです。

ja.yml
ja:
  delete: 削除
  retweet: リツイート
  favorite: お気に入り

方法

今回はルートパスに設定したviewの例のみ記します。
ここで対象になるのは[home.html.erb]です。

routes.rb
root to:'static-pages#home' #このhome-viewにi18nを適用します。
home.html.erb
<%=render 'microposts/micropost' %>

ルートの為、下記の記載で適用されます。

ja.yml
ja:
  delete: 削除
  retweet: リツイート
  favorite: お気に入り

パーシャルのlink_toに[t('文字列')]の形で書くとi18nが適用されます。
適用させたい箇所によって書き方が違うので調べたほうが吉。

_micropost.html.erb
<li id="micropost-<%= micropost.id %>">
<%= link_to t('delete'), micropost, method: :delete,data: { confirm: "You sure?" } %> #t('delete')
<%= link_to t('retweet'), retweet_path(id:micropost.id)%>
<%= link_to t('favorite'), favorite_path(id:micropost.id)%>
</li>

適用されれば[delete]が[削除]のように日本語で表示されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0