2
3

More than 3 years have passed since last update.

【Rails】日本語表記化(ページネーション、エラーメッセージ)

Last updated at Posted at 2020-04-25

railsで英語表記になっているものを日本語にする方法があります。

ページネーション

Railsのgemであるwill_paginateのラベルを日本語に変えます。

■デフォルト
スクリーンショット 2020-04-26 0.33.25.png

■日本語表記後
スクリーンショット 2020-04-26 0.43.19.png

実装

まずconfig/environment.rbに以下を記述します。

config/environment.rb
WillPaginate::ViewHelpers.pagination_options[:previous_label] = '&lt 前へ'
WillPaginate::ViewHelpers.pagination_options[:next_label] = '次へ &gt'

そして該当のページネーションに追記します。

<%= will_paginate %>
↓
<%= will_paginate , previous_label: '&lt 前へ', next_label: '次へ &gt'%>

これで実装できます。

エラーメッセージ

■デフォルト
image.png
■日本語化
image.png

実装

Gemfileにて下記をインストール

Gemfile
gem 'rails-i18n'

次にconfig/application.rbで以下を追記

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

これだけでも日本語表記化されますがモデルのカラム名などが英語のままです。
そこでconfig/locales内にja.ymlを追加し下記を入力してください。

config/locales/ja.yml
ja:
  activerecord:
    models:
      user: ユーザー
    attributes:
      user:
        name: 名前
        email: メールアドレス

これで名前とメールアドレスが日本語表記になります。

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