SSブログ

【Rails(3.2系)開発】deviseを利用して認証処理を作ってみる(日本語化) [rails]

前回に引き続きdeviseプラグインを利用した認証処理について。

【Rails(3.2系)開発】deviseを利用して認証処理を作ってみる(導入編)

今回は日本語化を進めてみます。

日本語化するのは以下の2点。

・テンプレート
・メッセージ

日本語化する方法も2つあって、1つは読み込むテンプレートを直接日本語に変更するやり方。
もう1つはrailsのi18nという多言語化の仕組みを利用するやり方。

今回はテンプレートは直接変更する方法で、メッセージ系はi18nを利用した多言語化ファイルを作成するやり方で進めて行きます。


まずはテンプレートの日本語化から。
まぁ直接テンプレートを上書きするだけなので、これを日本語化と言っていいか微妙だけど。

前回の導入編にてカスタマイズテンプレートを作成済なのでそれを元に変更します。
# カスタマイズテンプレートを生成する
% bundle exec rails g devise:views

app/views/devise配下につくられたテンプレートごとの機能は以下な感じ。
# ざっくりな訳なので間違ってたらゴメン
confirmations/
  # 正式登録の確認
  - new.html.erb
mailer/
  # 正式登録の案内
  - confirmation_instructions.html.erb
  # パスワード再発行の案内
  - reset_password_instructions.html.erb
  # アカウントロックの案内
  - unlock_instructions.html.erb
passwords/
  # パスワードの変更
  - edit.html.erb
  # パスワードの再発行
  - new.html.erb
registrations/
  # アカウント情報の更新/削除
  - edit.html.erb
  # アカウント情報の作成
  - new.html.erb
sessions/
  # ログイン(Sign in)
  - new.html.erb
shared/
  # footer部に表示されるリンク一覧
  - _links.erb
unlocks/
  # アカウントロックの解消
  - new.html.erb

とりあえずログイン(Sign in)ページだけ日本語で手直ししてみる。
% vim app/views/devise/sessions/new.html.erb
-----------------------------------------------------------------------

ログインページ

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
メールアドレス:
<%= f.email_field :email, :autofocus => true %>
パスワード:
<%= f.password_field :password %>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me %> 次回以降自動的にログインする
<% end -%>
<%= f.submit "ログインする" %>
<% end %> <%= render "devise/shared/links" %> -----------------------------------------------------------------------

shared/_links.html.erbは英語のままだけど気にしない。

WEBrickを起動してsign_inページを開いてみると日本語になっている事を確認(当たり前だけど)。

http://localhost:3000/users/sign_in
スクリーンショット 2013-04-21 19.38.17.png

この状態でemailやパスワードが空の状態でログインしようとすると英語のメッセージが表示されます。
次にこれらメッセージを日本語化します。

deviseのメッセージ類はdefaultで英語のi18nの多言語化ファイル(config/locales/devise.en.yml)が作成されています。

i18nとは何ぞや?となると思いますが、簡単に言うと1つのサイトを複数の言語に対応させるための仕組みです。
具体的には、それぞれの言語(国)別の辞書ファイルみたいなのを作成し、それを利用して多言語化を実現します。

なので日本語するためにはdeviseの日本語用の多言語化ファイルを作成する必要があります。
既にある英語の多言語化ファイルをベースに日本語化のファイルを作成します。
% cp config/locales/devise.en.yml config/locales/devise.ja.yml
% vim config/locales/devise.ja.yml
-----------------------------------------------------------------------
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
# Japanese translation for devise

ja:
  devise:
    confirmations:
      # Your account was successfully confirmed. You are now signed in.
      confirmed: "アカウントが確認されました。ログインしています。"
      # You will receive an email with instructions about how to confirm your account in a few minutes.
      send_instructions: "アカウントの確認方法を数分以内にメールでご連絡します。"
      # If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes.
      send_paranoid_instructions: "ご登録のメールアドレスが保存されている場合、アカウントの確認方法をメールでご連絡します。"
    failure:
      # You are already signed in.
      already_authenticated: "ログイン済みです。"
      # Your account was not activated yet.
      inactive: "アカウントがまだ有効になっていません。"
      # Invalid email or password.
      invalid: "メールアドレスまたはパスワードが違います。"
      # Invalid authentication token.
      invalid_token: "認証キーが不正です。"
      # Your account is locked.
      locked: "アカウントはロックされています。"
      # Invalid email or password.
      not_found_in_database: "メールアドレスまたはパスワードが無効です。"
      # Your session expired, please sign in again to continue.
      timeout: "一定時間が経過したため、再度ログインが必要です"
      # You need to sign in or sign up before continuing.
      unauthenticated: "ログインまたは登録が必要です。"
      # You have to confirm your account before continuing.
      unconfirmed: "本登録を行ってください。"
    mailer:
      confirmation_instructions:
        # Confirmation instructions
        subject: "アカウントの登録方法"
      reset_password_instructions:
        # Reset password instructions
        subject: "パスワードの再設定"
      unlock_instructions:
        # Unlock Instructions
        subject: "アカウントのロック解除"
    omniauth_callbacks:
      # Could not authenticate you from %{kind} because \"%{reason}\".
      failure: "%{kind} から承認されませんでした。理由:%{reason}"
      # Successfully authenticated from %{kind} account.
      success: "%{kind} から承認されました。"
    passwords:
      # You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided.
      no_token: "このページにアクセスする事が出来ません。正しいURLでアクセスしている事を確認して下さい。"
      # You will receive an email with instructions about how to reset your password in a few minutes.
      send_instructions: "パスワードのリセット方法を数分以内にメールでご連絡します。"
      # If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes.
      send_paranoid_instructions: "ご登録のメールアドレスが保存されている場合、パスワード復旧用のリンク先をメールでご連絡します。"
      # Your password was changed successfully. You are now signed in.
      updated: "パスワードを変更しました。ログイン済みです。"
      # Your password was changed successfully.
      updated_not_active: "パスワードを変更しました。"
    registrations:
      # Bye! Your account was successfully cancelled. We hope to see you again soon.
      destroyed: "ご利用ありがとうございました。アカウントが削除されました。またのご利用をお待ちしています。"
      # Welcome! You have signed up successfully.
      signed_up: "ようこそ! アカウントが登録されました。"
      # You have signed up successfully. However, we could not sign you in because your account is not yet activated.
      signed_up_but_inactive: "アカウントは登録されていますが、アクティブになっていないため利用できません。"
      # You have signed up successfully. However, we could not sign you in because your account is locked.
      signed_up_but_locked: "アカウントは登録されていますが、ロックされているため利用できません。"
      # A message with a confirmation link has been sent to your email address. Please open the link to activate your account.
      signed_up_but_unconfirmed: "確認メールを登録したメールアドレス宛に送信しました。リンクを開いてアカウントを有効にして下さい。"
      # You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address.
      update_needs_confirmation: "アカウント情報が更新されました。更新の確認メールを新しいメールアドレス宛に送信しましたので、リンクを開いて更新を有効にして下さい。"
      # You updated your account successfully.
      updated: "アカウントが更新されました。"
    sessions:
      # Signed in successfully.
      signed_in: "ログインしました。"
      # Signed out successfully.
      signed_out: "ログアウトしました。"
    unlocks:
      # You will receive an email with instructions about how to unlock your account in a few minutes.
      send_instructions: "アカウントのロックを解除する方法を数分以内にメールでご連絡します。"
      # If your account exists, you will receive an email with instructions about how to unlock it in a few minutes.
      send_paranoid_instructions: "アカウントが存在する場合、ロックを解除する方法をメールでご連絡します。"
      # Your account has been unlocked successfully. Please sign in to continue.
      unlocked: "アカウントのロックが解除されました。ログインしています。"
  errors:
    messages:
      # was already confirmed, please try signing in
      already_confirmed: "は既に登録済みです。ログインしてください"
      # needs to be confirmed within %{period}, please request a new one
      confirmation_period_expired: "%{period}以内に確認する必要がありますので、新しくリクエストしてください。"
      # has expired, please request a new one
      expired: "有効期限切れです。新規登録してください。"
      # not found
      not_found: "は見つかりませんでした。"
      # was not locked
      not_locked: "ロックされていません。"
      not_saved:
        # 1 error prohibited this %{resource} from being saved:
        one: "エラーにより、この %{resource} を保存できません:"
        # %{count} errors prohibited this %{resource} from being saved:
        other: "%{count} 個のエラーにより、この %{resource} を保存できません:"

-----------------------------------------------------------------------

devise.ja.ymlは、guthubなどで公開してくれている人のを参考に足りないトコは自分で訳す感じで作りました。
訳はだいぶざっくりなので意味合い違ってたらゴメン。

なお、利用しているdeviseのバージョンは2.2.3です。

参考にさせて貰ったdevise.ja.yml
github: Junsuke / miscellaneous / devise.ja.yml


作成が終わったら設定のlocaleを"ja"に変更して日本語の多言語化ファイルを読み込むようにします。
% vim config/application.rb
---------------------------------------
## 以下の行のコメントを外して設定値を":ja"に変更する

# config.i18n.default_locale = :de
↓↓↓
config.i18n.default_locale = :ja
---------------------------------------

WEBrickの起動、またはrailsを再起動してsign_inページを開きます。
% touch tmp/restart.txt

email、パスワード未入力などで何らかのメッセージが画面に表示される操作を行い、メッセージが日本語になっている事を確認します。

http://localhost:3000/users/sign_in
スクリーンショット_2013-04-27_21.21.53.png

細かいとこの日本語化は進めながらやるとして、とりあえず日本語化が出来ました。
nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。