LoginSignup
5
9

More than 5 years have passed since last update.

Rails4: deviseの日本語化とViewのカスタマイズ

Last updated at Posted at 2016-01-15

はじめに

deviseを利用し始めた際に、デフォルトでは各メッセージやラベルが英語なので、ここでは日本語にしたいと思います。

Railsのdefault_localeを日本語に設定

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

メッセージ

メッセージ用のロケールファイルの作成

  • deviseのインストールの初期コマンド実行時に、config/locales/devise.en.yml が生成されましたが、これの日本語版(devise.ja.yml)を作成します。
$ cp devise.en.yml devise.ja.yml 
devise.ja.yml
ja:
 ...
     sessions:
       #signed_in: "Signed in successfully."
       signed_in: "ログインしました。"

アクセス

  • 日本語になっていることを確認

020.png

ラベル

Viewのカスタマイズ

  • Viewのカスタマイズをするにあたって、rails g devise:install実行時の5番の対応を行います。
$ rails g devise:views
Running via Spring preloader in process 7679
      invoke  Devise::Generators::SharedViewsGenerator
      create    app/views/devise/shared
      create    app/views/devise/shared/_links.html.erb
      invoke  form_for
      create    app/views/devise/confirmations
      create    app/views/devise/confirmations/new.html.erb
      create    app/views/devise/passwords
      create    app/views/devise/passwords/edit.html.erb
      create    app/views/devise/passwords/new.html.erb
      create    app/views/devise/registrations
      create    app/views/devise/registrations/edit.html.erb
      create    app/views/devise/registrations/new.html.erb
      create    app/views/devise/sessions
      create    app/views/devise/sessions/new.html.erb
      create    app/views/devise/unlocks
      create    app/views/devise/unlocks/new.html.erb
      invoke  erb
      create    app/views/devise/mailer
      create    app/views/devise/mailer/confirmation_instructions.html.erb
      create    app/views/devise/mailer/password_change.html.erb
      create    app/views/devise/mailer/reset_password_instructions.html.erb
      create    app/views/devise/mailer/unlock_instructions.html.erb

Viewにベタ書きされている部分の変更

  • 1行目を「Sign up」から「ユーザ登録」に変更します。
app/views/devise/registrations/new.html.erb
  1 <h2>ユーザ登録</h2>
  2 
  3 <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  4   <%= devise_error_messages! %>

ラベルの変更

  • config/locales/ja.ymlを作成します。すでにある場合は追加します。
config/locales/ja.yml
ja:
  helpers:
    label:
      user:
        email: メールアドレス
        password: パスワード
        password_confirmation: パスワード(確認)
        remember_me: パスワードを記憶
...

アクセス

  • 日本語になっていることを確認

019.png

5
9
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
5
9