Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | モデルの各種項目の多言語対応

概要

モデルの各種項目の多言語対応

内容

モデルの各種項目の多言語対応をします。

モデルの多言語対応時は辞書に

activemodel:
  models:
    :model_name text
  attributes:
    model_name:
      attribute_name: text
activerecord:
  models:
    :model_name text
  attributes:
    model_name:
      attribute_name: text

の形式で指定します。

実装例

ja.yml

activerecord:
  errors:
    <<: *errors
  models:
    user: ユーザー
  attributes:
    user:
      id: id
      login: ログイン
      name: 名前
      password_digest: 暗号化パスワード
      password: パスワード
      password_confirmation: パスワード確認用
      email: Email

application_helperにヘルパーメソッドを追加

def tama(model, text)
  I18n.t("activemodel.attributes.#{model}.#{text}")
end

def tara(model, text)
  I18n.t("activerecord.attributes.#{model}.#{text}")
end