1
2

More than 3 years have passed since last update.

[Rails]エラーメッセージの日本語化

Posted at

概要

Railsアプリケーションのエラーメッセージを日本語化する手順を記します。
入植画面で未入力や不備があった場合に出力する下記の様なメッセージです。

bcb5b66aaa2336c94683fe2b94ccc49d.png

環境

Ruby 2.6.
Ruby on Rails 6.0.0

手順

①config/application.rbに記述

module Pictweet
 class Application < Rails::Application
   # Initialize configuration defaults for originally generated Rails version.
   config.load_defaults 6.0


   # 日本語の言語設定
   config.i18n.default_locale = :ja
  # 省略
 end
end

②gemfileに記述

gem rails-i18n'

bundle install実行

※ここまででカラム名以外は日本語になっています。

③config/locales/devise.en.ymlに以下URLのコードをコピペする
https://github.com/tigrish/devise-i18n/blob/master/rails/locales/ja.yml

※ここまででdeviseであらかじめ作成されるカラム(emai、password、password確認用)は日本語になっています。

④config/localesにja.yml作成

ja:
 activerecord:
   attributes:
     user:
       nickname: ニックネーム
     tweet:
       text: テキスト
       image: 画像

この記述で、Nickname、Text、Imageが日本語に翻訳されます!

1
2
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
1
2