Giter VIP home page Giter VIP logo

enum_translate's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.