Rails: How to use i18n with Rails 4 enums

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

    I've created a gem for this.

    http://rubygems.org/gems/translated_attribute_value

    Add to your gemfile:

    gem 'translated_attribute_value'
    

    If you have a status field for user:

    pt-BR:
      activerecord:
        attributes:
          user:
            status_translation:
              value1: 'Translation for value1'
              value2: 'Translation for value2'
    

    And in your view you can call like this:

    user.status_translated
    

    It works with active record, mongoid or any other class with getter/setters:

    https://github.com/viniciusoyama/translated_attribute_value

提交回复
热议问题