0.0
No commit activity in last 3 years
No release in over 3 years
Translate ActiveRecord::Enum attributes. It extends ActiveRecord methods like ActiveModel::Translation and `enumerize` gem do.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 2, >= 0.7
 Project Readme

EnumTranslate Build Status

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)
# => [["経済", "finance"],
#     ["生活豆知識", "lifehack"],
#     ["技術", "technology"]]

Instance Method

#human_attribute_text(:attribute_name)

article = Article.last
article.category
# => "technology"

article.human_attribute_text(:category)
# => "技術"

Rubygems

https://rubygems.org/gems/enum_translate

Contribute

PullRequests are welcome!

License

The gem is available as open source under the terms of the MIT License.