By combining the answers of Repolês and Aliaksandr , for Rails 5 we can build 2 methods that allow you to translate a single value or set of values from the enum attribute.
Set up translations in the .yml file:
en: activerecord: attributes: user: statuses: active: "Active" pending: "Pending" archived: "Archived"
In the ApplicationRecord class, from which all models are inherited, we define a method that processes translations for one value, and another that processes arrays by calling it:
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def self.translate_enum_name(enum_name, enum_value) I18n.t("activerecord.attributes.
In our representations, we can then translate the individual values:
<p>User Status: <%= User.translate_enum_name :status, @user.status %></p>
Or the whole set of enumeration values:
<%= f.select(:status, User.translate_enum_collection :status) %>
ne5t Feb 24 '17 at 18:42 2017-02-24 18:42
source share