0
0

More than 3 years have passed since last update.

【Rails】エラーメッセージ日本語化(メモ)

Last updated at Posted at 2021-09-02

 目標

エラーメッセージ日本語化

この英語のメッセージを日本語に変える。
スクリーンショット 2021-09-03 4.42.27.png

実装

まずgemfilerails-i18nを記述してインストールする。

Gemfile
gem 'rails-i18n'

bundle installします。

ターミナル
$ bundle install

config/application.rbファイルにconfig.i18n.default_locale = :jack_o_lantern:を記述します。

config/application.rb
config.i18n.default_locale = :jack_o_lantern: 

ローカルブラウザで確認するとcan't be blankで表示されていた部分が日本語仕様にに変わりました。
後はcontentstatusの名前部分を日本語に変えていきます。
スクリーンショット 2021-09-03 5.16.18.png

次に日本語変換用のファイルを作成していきます。

config/locales/配下にja.ymlファイルを作成します。
作成したファイルに contentstatus部分を日本語仕様にするように記述していきます。
モデル名指定、今回はtaksになっています。
あと日本語に変えるカラムも指定し記述していきます。

config/locales/ja.yml
ja:
    activerecord:
        attributes:
            task:
                content: 内容
                status: ステータス

これでconfig/locales/ja.ymlで指定したcontentstatus部分が日本語に変わりました。
スクリーンショット 202to1-09-03 5.43.22.png

以上になります。

0
0
1

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