Git Product home page Git Product logo

translate_enum's Introduction

Build Status Code Climate

TranslateEnum

Simple, zero-dependant enum translation gem for Rails

Installation

gem install translate_enum

Usage

Here is a regular use case. ActiveRecord model:

class Post < ActiveRecord::Base
  include TranslateEnum
  
  enum status: { published: 0, archive: 1 }
  translate_enum :status
end

Localization file

en:
  activerecord:
    attributes:
      post:
        status_list:
          published: Was published
          archive: Was archived

Or if you wish your locales to be available across all models

en:
  attributes:
    status_list:
      published: Was published
      archive: Was archived
Post.translated_status(:published) #=> "Was published"
Post.translated_statuses => [["Was published", :published, 0], ["Was archived", :archive, 1]]

@post = Post.new(status: :published)
@post.translated_status #=> "Was published"

Use in a Form

= form_for @post do |f|
  = f.select :status, options_for_select(f.object.class.translated_statuses.map { |translation, k, _v| [translation, k] })

Extending ActiveRecord

Be default you should extend each ActiveRecord model manually by including TranslateEnum module in it. You can extend ActiveRecord by requiring translate_enum/active_record in initializer or inside yout Gemfile:

Gemfile:

gem 'translate_enum', require: 'translate_enum/active_record'

Initializer:

# config/initializers/translate_enum.rb
require 'translate_enum/active_record'

Advanced options

class User < ActiveRecord::Base
  enum gender: [:male, :female]
  
  translate_enum :gender do |tr|
    tr.i18n_scope = 'activerecord.attributes'
    tr.i18n_key = 'gender_list'
    tr.enum_klass_method_name = 'genders'
    tr.enum_instance_method_name = 'gender'
    tr.method_name_singular = 'translated_gender'
    tr.method_name_plural = 'translated_genders'
  end
  
  # Or even provide your own logic
  def self.translated_gender(key)
    I18n.t(key, scope: 'global.gender_list')
  end
end

How To?

How use translate enum in serializer

Example for Grape:

class Feedback < ApplicationRecord
  include TranslateEnum
  
  enum topic: {
    question: 'question', issue: 'issue', complaint: 'complaint', offer: 'offer',
    investment: 'investment'
  }

  translate_enum :topic
end
class FeedbacksApi < Grape::API
  resource :feedbacks do
    get 'enums' do
      present Feedback.method(:translated_topics), with: TranslateEnumSerializer
    end
  end
end
class TranslateEnumSerializer < Grape::Entity
  expose :enum, as: ->(method) { method.name[/translated_(.*)/, 1] } do |method|
    method.call.map do |translation, key, _value|
      { value: key, translation: translation }
    end
  end
end
curl http://localhost:3000/feedbacks/enums
{
  "topics": [
    {
      "value": "question",
      "translation": "Vopros"
    },
    {
      "value": "issue",
      "translation": "Problema"
    },
    {
      "value": "complaint",
      "translation": "Zhaloba"
    },
    {
      "value": "offer",
      "translation": "Predlozhenie"
    },
    {
      "value": "investment",
      "translation": "Invisticii"
    }
  ]
}

translate_enum's People

Contributors

shlima avatar zerocool4u2 avatar karwank avatar coderbydesign avatar

Watchers

James Cloos avatar

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.