Rails: How to use i18n with Rails 4 enums

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

    I prefer a simple helper in application_helper

      def translate_enum(object, enum_name)
        I18n.t("activerecord.attributes.#{object.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{object.send(enum_name)}")
      end
    

    Then in my YML file :

    fr:
      activerecord:
        attributes:
          my_model:
            my_enum_plural:
              pending:  "En cours"
              accepted: "Accepté"
              refused:  "Refusé"
    

提交回复
热议问题