i18n の設定 : 小林研 Rails Tips (49)

はじめに

Rails Tips の 49 回目です。次は何を記述しようと config を覗いていたら config/applicatoin.rbtime_zone の表記を見つけました。せっかくなので、i18n の設定も追記しましょう。

Rails をはじめよう - Railsガイド

config/application.rb の修正

time_zone を設定するコメントがあったので、その場所に Asia/Tokyo のタイムゾーンを加えました。そこについでに、i18 に関する以下の設定を追加します。

  • default_locale: 指定がない場合に :ja (日本語) を表示するようにします。
  • load_path: config/locales の下にフォルダを作成して yml を設置するので、それらが読み込まれるようにします。
  • fallbacks: 指定された言語の翻訳がなかったときにどの言語のものを代わりに表示するかを指定します。今回は default_locale なので日本語になります。
    # config.time_zone = "Central Time (US & Canada)"
    config.time_zone = "Asia/Tokyo"
    config.i18n.default_locale = :ja
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]
    config.i18n.fallbacks = [I18n.default_locale]

config/locales/ja.yml の準備

デフォルトの状態で config/locales/en.yml は用意されています。ここに基本となる ja.yml を用意します。基本となるものは以下のリポジトリで配布されています。

日本語翻訳文字列

ここで Raw とすると以下のような URL が取得できます。

https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml

このファイルを config/locales/ja.yml として保存したいので、curl で保存してみましょう。

curl -o config/locales/ja.yml https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml

現在はこんな yaml ファイルが取得されました。その他 model, view に関する翻訳は後日フォルダに分けて保存することにしましょう。

---
ja:
  activerecord:
    errors:
      messages:
        record_invalid: 'バリデーションに失敗しました: %{errors}'
        restrict_dependent_destroy:
          has_one: "%{record}が存在しているので削除できません"
          has_many: "%{record}が存在しているので削除できません"
  date:
    abbr_day_names:
    - - - - - - - abbr_month_names:
    - 
    - 1月
    - 2月
    - 3月
    - 4月
    - 5月
    - 6月
    - 7月
    - 8月
    - 9月
    - 10月
    - 11月
    - 12月
    day_names:
    - 日曜日
    - 月曜日
    - 火曜日
    - 水曜日
    - 木曜日
    - 金曜日
    - 土曜日
    formats:
      default: "%Y/%m/%d"
      long: "%Y年%m月%d日(%a)"
      short: "%m/%d"
    month_names:
    - 
    - 1月
    - 2月
    - 3月
    - 4月
    - 5月
    - 6月
    - 7月
    - 8月
    - 9月
    - 10月
    - 11月
    - 12月
    order:
    - :year
    - :month
    - :day
  datetime:
    distance_in_words:
      about_x_hours: 約%{count}時間
      about_x_months: 約%{count}ヶ月
      about_x_years: 約%{count}年
      almost_x_years: "%{count}年弱"
      half_a_minute: 30秒前後
      less_than_x_seconds: "%{count}秒未満"
      less_than_x_minutes: "%{count}分未満"
      over_x_years: "%{count}年以上"
      x_seconds: "%{count}秒"
      x_minutes: "%{count}分"
      x_days: "%{count}日"
      x_months: "%{count}ヶ月"
      x_years: "%{count}年"
    prompts:
      second:minute:hour:day:month:year:errors:
    format: "%{attribute}%{message}"
    messages:
      accepted: を受諾してください
      blank: を入力してください
      confirmation: と%{attribute}の入力が一致しません
      empty: を入力してください
      equal_to: は%{count}にしてください
      even: は偶数にしてください
      exclusion: は予約されています
      greater_than: は%{count}より大きい値にしてください
      greater_than_or_equal_to: は%{count}以上の値にしてください
      in: は%{count}の範囲に含めてください
      inclusion: は一覧にありません
      invalid: は不正な値です
      less_than: は%{count}より小さい値にしてください
      less_than_or_equal_to: は%{count}以下の値にしてください
      model_invalid: 'バリデーションに失敗しました: %{errors}'
      not_a_number: は数値で入力してください
      not_an_integer: は整数で入力してください
      odd: は奇数にしてください
      other_than: は%{count}以外の値にしてください
      present: は入力しないでください
      required: を入力してください
      taken: はすでに存在します
      too_long: は%{count}文字以内で入力してください
      too_short: は%{count}文字以上で入力してください
      wrong_length: は%{count}文字で入力してください
    template:
      body: 次の項目を確認してください
      header: "%{model}に%{count}個のエラーが発生しました"
  helpers:
    select:
      prompt: 選択してください
    submit:
      create: 登録する
      submit: 保存する
      update: 更新する
  number:
    currency:
      format:
        delimiter: ","
        format: "%n%u"
        precision: 0
        separator: "."
        significant: false
        strip_insignificant_zeros: false
        unit:format:
      delimiter: ","
      precision: 3
      round_mode: default
      separator: "."
      significant: false
      strip_insignificant_zeros: false
    human:
      decimal_units:
        format: "%n %u"
        units:
          billion: 十億
          million: 百万
          quadrillion: 千兆
          thousand:trillion:unit: ''
      format:
        delimiter: ''
        precision: 3
        significant: true
        strip_insignificant_zeros: true
      storage_units:
        format: "%n%u"
        units:
          byte: バイト
          eb: EB
          gb: GB
          kb: KB
          mb: MB
          pb: PB
          tb: TB
    percentage:
      format:
        delimiter: ''
        format: "%n%"
    precision:
      format:
        delimiter: ''
  support:
    array:
      last_word_connector: "、"
      two_words_connector: "、"
      words_connector: "、"
  time:
    am: 午前
    formats:
      default: "%Y年%m月%d日(%a) %H時%M分%S秒 %z"
      long: "%Y/%m/%d %H:%M"
      short: "%m/%d %H:%M"
    pm: 午後

おわりに

とりあえず基本となる i18n の設定をしました。その他の yaml はその度ごとに追加していきます。