STUDY MEMO

学習のメモ書き

<Ruby on Rails> コントローラでのRails 国際化 (i18n) API使用

flashの文字を日本語で表示させた。

1.gemを追加し、bundle install

gem 'rails-i18n'



2.config/application.rbに以下を追記(class Application < Rails::Application内)

config.i18n.default_locale = :ja



3.config/locates/にja.ymlを作成し、以下を記載

ja:
  #コントローラ名
  posts:
    success: 投稿に成功しました。
    update_success: 内容を更新しました。



4.controller.rbのnoticeにtranslateメソッドを記載

#createアクション
~
redirect_to ルートパス, notice: t('.success')
~

#updateアクション
~
redirect_to ルートパス, notice: t('.supdate_success')
~

上記手順で以下のように表示される。

https://i.gyazo.com/9c2cb041ad607bcef8ea2ffda20bf9a9.png

https://i.gyazo.com/b090138b4bddde973977ec2b16db8ddf.png

参考:探索の「遅延」(lazy lookup) railsguides.jp