Rails: How to use i18n with Rails 4 enums

后端 未结 16 2235
天涯浪人
天涯浪人 2020-11-28 04:10

Rails 4 Active Record Enums are great, but what is the right pattern for translating with i18n?

16条回答
  •  自闭症患者
    2020-11-28 04:47

    The model:

    class User < ActiveRecord::Base
      enum role: [:master, :apprentice]
    end
    

    The locale file:

    en:
      activerecord:
        attributes:
          user:
            master: Master
            apprentice: Apprentice
    

    Usage:

    User.human_attribute_name(:master) # => Master
    User.human_attribute_name(:apprentice) # => Apprentice
    

提交回复
热议问题