学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails7.1 | 動画学習アプリ作成 | 08 | 日本語化

↓↓クリックして頂けると励みになります。




07 | ログイン機能の実装】 << 【ホーム】 >> 【09 | Notyの利用



Rails日本語化
Rails日本語化


Ruby on Railsを日本語化するには、以下の手順を実行します。
Ruby on Railsは多言語対応が得意であり、多くの部分を簡単に翻訳できます。


Rails7.1はI18n(Internationalization、国際化)をサポートしています。


GemFileに以下のgemを追加します。

gem 'rails-i18n', '~> 7.0', '>= 7.0.8'



コマンド
bundle


「config\locales」フォルダに「devise.ja.yml」ファイルを新規作成します。


config\locales\devise.ja.yml(新規作成したファイル)

ja:
  devise:
    confirmations:
      confirmed: 'アカウントを登録しました。'
      send_instructions: 'アカウントの有効化について数分以内にメールでご連絡します。'
      send_paranoid_instructions: "あなたのメールアドレスが登録済みの場合、本人確認用のメールが数分以内に送信されます。"
    failure:
      already_authenticated: 'すでにログインしています。'
      inactive: 'アカウントが有効化されていません。メールに記載された手順にしたがって、アカウントを有効化してください。'
      invalid: "%{authentication_keys} もしくはパスワードが不正です。"
      locked: 'あなたのアカウントは凍結されています。'
      last_attempt: 'あなたのアカウントが凍結される前に、複数回の操作がおこなわれています。'
      not_found_in_database: "%{authentication_keys} もしくはパスワードが不正です。"
      timeout: 'セッションがタイムアウトしました。もう一度ログインしてください。'
      unauthenticated: 'アカウント登録もしくはログインしてください。'
      unconfirmed: 'メールアドレスの本人確認が必要です。'
    mailer:
      confirmation_instructions:
        subject: 'アカウントの有効化について'
      reset_password_instructions:
        subject: 'パスワードの再設定について'
      unlock_instructions:
        subject: 'アカウントの凍結解除について'
      password_change:
        subject: 'パスワードの変更について'
    omniauth_callbacks:
      failure: "%{kind} アカウントによる認証に失敗しました。理由:(%{reason})"
      success: "%{kind} アカウントによる認証に成功しました。"
    passwords:
      no_token: "このページにはアクセスできません。パスワード再設定メールのリンクからアクセスされた場合には、URL をご確認ください。"
      send_instructions: 'パスワードの再設定について数分以内にメールでご連絡いたします。'
      send_paranoid_instructions: "あなたのメールアドレスが登録済みの場合、パスワード再設定用のメールが数分以内に送信されます。"
      updated: 'パスワードが正しく変更されました。'
      updated_not_active: 'パスワードが正しく変更されました。'
    registrations:
      destroyed: 'アカウントを削除しました。またのご利用をお待ちしております。'
      signed_up: 'アカウント登録が完了しました。'
      signed_up_but_inactive: 'ログインするためには、アカウントを有効化してください。'
      signed_up_but_locked: 'アカウントが凍結されているためログインできません。'
      signed_up_but_unconfirmed: '本人確認用のメールを送信しました。メール内のリンクからアカウントを有効化させてください。'
      update_needs_confirmation: 'アカウント情報を変更しました。変更されたメールアドレスの本人確認のため、本人確認用メールより確認処理をおこなってください。'
      updated: 'アカウント情報を変更しました。'
    sessions:
      signed_in: 'ログインしました。'
      signed_out: 'ログアウトしました。'
      already_signed_out: '既にログアウト済みです。'
    unlocks:
      send_instructions: 'アカウントの凍結解除方法を数分以内にメールでご連絡します。'
      send_paranoid_instructions: 'アカウントが見つかった場合、アカウントの凍結解除方法を数分以内にメールでご連絡します。'
      unlocked: 'アカウントを凍結解除しました。'
  errors:
    messages:
      already_confirmed: 'は既に登録済みです。ログインしてください。'
      confirmation_period_expired: "の期限が切れました。%{period} までに確認する必要があります。 新しくリクエストしてください。"
      expired: 'の有効期限が切れました。新しくリクエストしてください。'
      not_found: 'は見つかりませんでした。'
      not_locked: 'は凍結されていません。'
      not_saved:
        one: "エラーが発生したため %{resource} は保存されませんでした:"
        other: "%{count} 件のエラーが発生したため %{resource} は保存されませんでした:"



config/application.rb ファイル内でI18nを有効にします。
また、タイムゾーンの変更も合わせて行います。
以下のように設定します。


記述追加 config\application.rb
28行目に「config.i18n.default_locale = :ja」の記述を追加しています。
31行目に「config.time_zone = 'Tokyo'」の記述を追加しています。

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 GigHub7
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.1

    # Please, add to the `ignore` list any other `lib` subdirectories that do
    # not contain `.rb` files, or that should not be reloaded or eager loaded.
    # Common ones are `templates`, `generators`, or `middleware`, for example.
    config.autoload_lib(ignore: %w(assets tasks))

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    # config.eager_load_paths << Rails.root.join("extras")

    # 日本語化
    config.i18n.default_locale = :ja
    
    # 標準時間を東京に設定
    config.time_zone = 'Tokyo'    

  end
end



バリデーションの日本語化、日付フォーマットを設定しておきます。


「config\locales」フォルダに「ja.yml」ファイルを新規作成して下さい。


config\locales\ja.yml(新規作成したファイル)

ja:
  activerecord:
    attributes:
      user:
        full_name: 氏名
        current_password: パスワード
        password_confirmation: パスワードの確認
        password: パスワード
        email: メールアドレス
        phone_number: 電話番号

  time:
    formats:
        full_date: "%Y年%m月%d日 (%a)"
  views:
    pagination:
      first: "&laquo; 最初"
      last: "最後 &raquo;"
      previous: "&lsaquo; 前"
      next: "次 &rsaquo;"
      truncate: "..."
  helpers:
    page_entries_info:
      one_page:
        display_entries:
          zero: ""
          one: "<strong>1-1</strong>/1件中"
          other: "<strong>1-%{count}</strong>/%{count}件中"
      more_pages:
        display_entries: "<strong>%{first}-%{last}</strong>/%{total}件中"
  datetime:
    distance_in_words:
      half_a_minute: "30秒前後前"
      less_than_x_seconds:
        one:   "1秒前"
        other: "%{count}秒前"
      x_seconds:
        one:   "1秒"
        other: "%{count}秒前"
      less_than_x_minutes:
        one:   "1分"
        other: "%{count}分前"
      x_minutes:
        one:   "約1分前"
        other: "%{count}分前"
      about_x_hours:
        one:   "約1時間前"
        other: "約%{count}時間前"
      x_days:
        one:   "1日前"
        other: "%{count}日前"
      about_x_months:
        one:   "約1ヶ月前"
        other: "約%{count}ヶ月前"
      x_months:
        one:   "1ヶ月前"
        other: "%{count}ヶ月前"
      almost_x_years:
        one:   "1年弱前"
        other: "%{count}年弱前"
      about_x_years:
        one:   "約1年前"
        other: "約%{count}年前"
      over_x_years:
        one:   "1年以上前"
        other: "%{count}年以上前"        



これらの手順に従うと、Ruby on Railsアプリケーションを日本語に簡単にローカライズできます。
翻訳ファイルは必要に応じて拡充し、アプリケーション内のすべてのテキストを日本語に対応させることができます。


07 | ログイン機能の実装】 << 【ホーム】 >> 【09 | Notyの利用




↓↓クリックして頂けると励みになります。