Rails: How to use i18n with Rails 4 enums

后端 未结 16 2321
天涯浪人
天涯浪人 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:43

    You can simply add a helper:

    def my_something_list
      modes = 'activerecord.attributes.mymodel.my_somethings'
      I18n.t(modes).map {|k, v| [v, k]}
    end
    

    and set it up as usually:

    en:
      activerecord:
        attributes:
          mymodel:
            my_somethings:
               my_enum_value: "My enum Value!"
    

    then use it with your select: my_something_list

提交回复
热议问题