EnumTranslate
EnumTranslate is a gem to translate ActiveRecord::Enum defined attributes with i18n system.
Installation
Add this line to your application's Gemfile:
gem 'enum_translate'
Usage
Preparation
If you have an ActiveRecord model like this
class Article < ActiveRecord::Base
enum category: { finance: 0, lifehack: 1, technology: 2 }
end
include EnumTranslate
module as a concern
class Article < ActiveRecord::Base
include EnumTranslate
enum category: { finance: 0, lifehack: 1, technology: 2 }
end
And if you set i18n
locale as ja
I18n.locale = :ja
Please make a locale file like this
ja:
activerecord:
attributes:
article/category:
finance: 経済
lifehack: 生活豆知識
technology: 技術
APIs
Class Method
.human_attribute_options(:attribute_name)
Article.human_attribute_options(:category)
Instance Method
#human_attribute_text(:attribute_name)
article = Article.last
article.category
article.human_attribute_text(:category)
Contribute
PullRequests are welcome!
License
The gem is available as open source under the terms of the MIT License.