7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

simple_formでnestしたmodelの多言語化対応

Posted at

simple_fromでuserが複数profileを持っているような場合、localeファイルは、親model配下にnestした上で複数系で指定する。

user.rb
class User < ActiveRecord::Base
  has_many :profiles, dependent: :destroy
  accepts_nested_attributes_for :profiles
  # …
end
profile.rb
class Profile < ActiveRecord::Base
  belongs_to :user
  # …
end

正解

config/locales/simple_form.ja.yml
ja:
  simple_form:
    # ...
    labels:
      defaults:
      # ...
      user:
        # ...
        profiles:
          hoge: "ほげ"
          huga: "ふが"

不正解

親modelと同じ階層

config/locales/simple_form.ja.yml
ja:
  simple_form:
    # ...
    labels:
      defaults:
      # ...
      user:
      # ...
      profile:
        hoge: "ほげ"
        huga: "ふが"

単数形

config/locales/simple_form.ja.yml
ja:
  simple_form:
    # ...
    labels:
      defaults:
      # ...
      user:
        # ...
        profile:
          hoge: "ほげ"
          huga: "ふが"
7
5
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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?