0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】エラーメッセージを日本語化

Last updated at Posted at 2024-12-06

記事概要

Railsでエラーメッセージを日本語にする方法を説明する。

言語やフレームワーク

使用技術
フロントエンド HTML
CSS
バックエンド Ruby 3.2.0
Ruby on Rails 7.0.8.6
データベース MySQL
インフラ -
API -
その他 -

前提

  • モデルにバリデーションが設定されていること

手順(コントローラーの処理分岐)

  1. データ保存・更新に成功した場合と失敗した場合に、コントローラーで処理を分岐する
    class ItemsController < ApplicationController
      # 中略
      def create
        # Itemデータを作成
        @item = Item.new(set_params)
        if @item.save
          # データ保存に成功した場合の処理
          redirect_to root_path
        else
          # データ保存に失敗した場合の処理
          render :new, status: :unprocessable_entity
        end
      end
    
      # 中略
    
      def update
        if @item.update(set_params)
          # データ更新に成功した場合の処理
          redirect_to item_path(@item.id)
        else
          # データ更新に失敗した場合の処理
          render :edit, status: :unprocessable_entity
        end
      end
    
      # 中略
    end
    

手順(エラー処理を表示)

エラーメッセージ用ビューファイルを利用するケース

  1. エラーメッセージ用の部分テンプレートapp/views/shared/_error_messages.html.erbを手動作成し、下記のように記述する
    <% if model.errors.any? %>
    <div class="error-alert">
      <ul>
        <% model.errors.full_messages.each do |message| %>
        <li class='error-message'><%= message %></li>
        <% end %>
      </ul>
    </div>
    <% end %>
    
  2. エラーメッセージを表示させる部分に、下記を追記する
    <!-- 省略 -->
    
    <%= render 'shared/error_messages', model: f.object %>
    
    <!-- 省略 -->
    
    ※この時点でブラウザ画面を確認すると、英語でエラーメッセージが表示される

フラッシュメッセージを利用するケース

  1. フラッシュメッセージを表示させる部分に、下記を追記する
    <!-- 省略 -->
    
    <%= flash[:notice] %>
    <%= flash[:alert] %>
    
    <!-- 省略 -->
    
    ※この時点でブラウザ画面を確認すると、英語でエラーメッセージが表示される

手順(エラーメッセージの日本語化)

  1. config/application.rbの言語設定を変更するため、config.i18n.default_locale = :jaを追記する
    application.rb
    module Pictweet
      class Application < Rails::Application
        # 中略
        config.load_defaults 7.0
    
        # 日本語の言語設定を追記
        config.i18n.default_locale = :ja
        
        # 中略
        end
    end
    

手順(Gemの追加)

  1. Gemrails-i18nを追加する
    詳細は、こちらを参照
  2. ブラウザ画面を確認すると、日本語でエラーメッセージが表示される。ただし、項目名は英語のまま

手順(項目名の日本語化)

  1. config/locales/devise.ja.ymlを手動作成する
  2. devise-i18nのコードを全てコピーし、config/locales/devise.ja.ymlに貼り付ける
    ※インデントにミスがあるとエラー発生するので、注意
  3. ブラウザ画面を確認すると、Gem「devise」の項目は日本語になっているが、自身で設定した項目は英語表記のまま

手順(項目名の日本語化2)

  1. config/locales/ja.ymlを手動作成する
    ja.yml
    ja:
      # モデル項目を日本語に変換
      activerecord:
        attributes:
          # Gem「devise」で作成したUserモデル
          user:
            # Userモデルのnicknameを「ニックネーム」と表示
            nickname: ニックネーム
          item:
            # Itemモデルのnameを「商品名」と表示
            name: 商品名
            
            # ActiveHashを使用している場合、idに対して日本語を設定
            category_id: カテゴリー
    
            # 'has_one_attached'で紐づけている画像に対して、日本語を設定
            image: 商品画像
            
            # 'belongs_to'で紐づけている外部キーに対して、日本語を設定
            user: ユーザー
    
      # Formオブジェクトのモデル項目を日本語に変換
      activemodel:
        attributes:
          order_ship:
            post_code: 郵便番号
            prefecture_id: 都道府県
            token: クレジットカード情報
            user: ユーザー
            item: 商品
    
  2. ブラウザ画面を確認すると、エラーメッセージが全て日本語で表示される

手順(モデル単体テストを英語表記に修正)

  1. spec/rails_helper.rbI18n.locale = "en"が記述されている場合、I18n.locale = "ja"に修正する

手順(モデル単体テストコードを修正)

  1. spec/models/user_spec.rbなどに記載しているエラーメッセージを日本語に変更する
    • 修正前
      user_spec.rb
      # 中略
      context 'ユーザー登録できないとき' do
        it 'emailが空では登録できない' do
          @user.email = ''
          @user.valid?
          expect(@user.errors.full_messages).to include('Email can't be blank')
        end
      # 中略
      
    • 修正後
      user_spec.rb
      # 中略
      context 'ユーザー登録できないとき' do
        it 'emailが空では登録できない' do
          @user.email = ''
          @user.valid?
          expect(@user.errors.full_messages).to include('Eメールを入力してください')
        end
      # 中略
      

備考

  • Formオブジェクトの外部キーはアソシエーションを組めないという特性により、項目名を日本語にできない
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?