LoginSignup
3
4

More than 5 years have passed since last update.

Rails 多言語化対応 Model編

Last updated at Posted at 2017-07-27

はじめに

今後のサービス化していくシステムは多言語化対応が必須となってきます。
なので、多言語化の方針と記述方法を簡単にまとめたので目を通してください。
View編を先に一読ください↓
Rails 多言語化対応 View編

参考サイト

以下のサイトを参考にしました。
記事を読む前に一読お願いします。
モデル名やモデルの属性名の多言語化

実装

dictionary

1つのファイルに全文言をかくと意味不明状態になるので、
参考サイトに倣って然るべき場所に然るべきファイルを作成し文言を記述していきます。
今回はbrandのvalidationで使いたかったので

  • config/locales/models/brands/ja.yml
  • config/locales/models/brands/en.yml

を作成しています。

config/locales/models/brands/ja.yml
ja:
  activerecord:
    models:
      brand: ブランド
    attributes:
      brand:
        id: ブランドID
        name: ブランド名
        name_en: ブランド名(英語)
        sellable_jp: Sellable JP
        sellable_us: Sellable US
        brand_category_id: ブランドカテゴリー
config/locales/models/brands/en.yml
en:
  activerecord:
    models:
      brand: Brand
    attributes:
      brand:
        id: Brand ID
        name: Brand Name
        name_en: Brand Name(English)
        sellable_jp: Sellable JP
        sellable_us: Sellable US
        brand_category_id: Brand Category

これ以外にも階層を持った書き方もできたりします。
詳しくは調べてください。

結果

locale = :jaの場合
スクリーンショット 2017-07-27 21.45.16.png

locale = :enの場合
スクリーンショット 2017-07-27 21.45.29.png

補足

「を入力してください」の部分はvalidatesを使えば自動で変換されます。

3
4
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
3
4