LoginSignup
2
1

More than 3 years have passed since last update.

[日本語化]i18n

Last updated at Posted at 2020-01-08

日本語化したい( ・∇・)

1,application.rbに記述

2,ymlファイルを作成して中身をコピペする

「config/locales」フォルダに、「devise.ja.yml」および「ja.yml」という名前のファイルを新規作成

devise.ja.yml
ja.yml

3,終了(必要に応じてyml中身の記述を増やしたり変えてください)

フラッシュなどの日本語化はこれでOKですがモデルのバリデーションなどはこれでは変わりません。

なのでGemインストールして実装していきます

1,Gem記述してbundle

Gemfile
gem 'rails-i18n'

2,application.rbに記述

config/application.rb
#変更前
require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

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

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

#変更後
require_relative 'boot'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

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

  #この下を追加したよ(この2行は時間が日本時間になるだけだから書かなくてもOK)
    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local

    #以下の記述を追記する(設定必須!!!)
    config.i18n.default_locale = :ja # デフォルトのlocaleを日本語(:ja)にする
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]#パスを通す
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

3,ファイルの追加

config/locales/device.ja.yml
ja:
  activerecord:
    attributes:
      user:
        confirmation_sent_at: パスワード確認送信時刻
        confirmation_token: パスワード確認用トークン
        confirmed_at: パスワード確認時刻
        created_at: 作成日
        current_password: 現在のパスワード
        current_sign_in_at: 現在のログイン時刻
        current_sign_in_ip: 現在のログインIPアドレス
        email: Eメール
        encrypted_password: 暗号化パスワード
        failed_attempts: 失敗したログイン試行回数
        last_sign_in_at: 最終ログイン時刻
        last_sign_in_ip: 最終ログインIPアドレス
        locked_at: ロック時刻
        password: パスワード
        password_confirmation: パスワード(確認用)
        remember_created_at: ログイン記憶時刻
        remember_me: ログインを記憶する
        reset_password_sent_at: パスワードリセット送信時刻
        reset_password_token: パスワードリセット用トークン
        sign_in_count: ログイン回数
        unconfirmed_email: 未確認Eメール
        unlock_token: ロック解除用トークン
        updated_at: 更新日
        password: パスワード
        last_name_kana: 苗字(カナ)
        first_name_kana: 名前(カナ)
      address:
        postal_code: 郵便番号
        prefectures: 都道府県
        city: 市区町村
        address: 番地
        building: 建物名

addressモデルとuserモデルのところ追加しました

参考ページ

[初学者]Railsのi18nによる日本語化対応

Railsのバリデーションエラーのメッセージの日本語化

I18n でネストしたモデルの属性名の対応

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