Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 586
swordray Avatar asked Oct 20 '22 10:10

swordray


2 Answers

Try the enum_help gem. From its description:

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

like image 192
Kukunin Avatar answered Oct 23 '22 01:10

Kukunin


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

like image 39
pierrea Avatar answered Oct 23 '22 01:10

pierrea