swordray
swordray

Reputation: 842

How to write i18n String for Rails 4.1 Enums?

For exmaple, edit config/locales/en.yml

en:
  activerecord:
    models:
      user: User
    attributes:
      user:
        name: Name

You can get

User.model_name.human # => User

User.human_attribute_name('name') # => Name

But how to write Rails 4.1 Enums I18n values ?

Upvotes: 5

Views: 3657

Answers (2)

Kukunin
Kukunin

Reputation: 883

Try the enum_help gem. From its description:

Help ActiveRecord::Enum feature to work fine with I18n and simple_form.

Upvotes: 2

pierrea
pierrea

Reputation: 1487

I didn't find any specific pattern either, so I simply added:

en:
  user_status:
    active:   Active
    pending:  Pending...
    archived: Archived

to an arbitrary .yml file. Then in my views:

I18n.t :"user_status.#{user.status}"

cf. https://stackoverflow.com/a/23104850/797639

Upvotes: 1

Related Questions