Rails: How to use i18n with Rails 4 enums

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

    Model:

    enum stage: { starting: 1, course: 2, ending: 3 }
    
    def self.i18n_stages(hash = {})
      stages.keys.each { |key| hash[I18n.t("checkpoint_stages.#{key}")] = key }
      hash
    end
    

    Locale:

    checkpoint_stages:
        starting: Saída
        course: Percurso
        ending: Chegada
    

    And on the view (.slim):

    = f.input_field :stage, collection: Checkpoint.i18n_stages, as: :radio_buttons
    

提交回复
热议问题