All Projects → shlima → Translate_enum

shlima / Translate_enum

Licence: mit
Easily Translate Enums in Rails

Programming Languages

ruby
36898 projects - #4 most used programming language
enum
40 projects

Labels

Projects that are alternatives of or similar to Translate enum

Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+615.56%)
Mutual labels:  i18n, rails
I18n Debug
Ever wondered which translations are being looked up by Rails, a gem, or simply your app? Wonder no more!
Stars: ✭ 143 (+58.89%)
Mutual labels:  i18n, rails
Texterify
The localization management system.
Stars: ✭ 37 (-58.89%)
Mutual labels:  i18n, rails
Spree i18n
I18n translation files for Spree Commerce.
Stars: ✭ 338 (+275.56%)
Mutual labels:  i18n, rails
Ordinalize full
Turns a number into an ordinal string such as first, second, third or 1st, 2nd, 3rd.
Stars: ✭ 6 (-93.33%)
Mutual labels:  i18n, rails
Ununiga
[은는이가] 한글 조사(助詞) 대응 I18n engine extension
Stars: ✭ 34 (-62.22%)
Mutual labels:  i18n, rails
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+1174.44%)
Mutual labels:  i18n, rails
Rails Security Checklist
🔑 Community-driven Rails Security Checklist (see our GitHub Issues for the newest checks that aren't yet in the README)
Stars: ✭ 1,265 (+1305.56%)
Mutual labels:  rails
Covid Volunteers
Organizing and matching volunteers with COVID-19 projects
Stars: ✭ 87 (-3.33%)
Mutual labels:  rails
Embedding Reference Apps
Reference applications for common web frameworks showing how to embed Metabase charts
Stars: ✭ 83 (-7.78%)
Mutual labels:  rails
Snibox
Self-hosted snippet manager
Stars: ✭ 1,247 (+1285.56%)
Mutual labels:  rails
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+1304.44%)
Mutual labels:  rails
Simpleadmin Rails
SimpleAdmin - Dashboard for modern website without wasting time
Stars: ✭ 87 (-3.33%)
Mutual labels:  rails
Thredded
The best Rails forums engine ever.
Stars: ✭ 1,263 (+1303.33%)
Mutual labels:  rails
Muvee
μv: (mew-vee) Netflix, for your home. WIP
Stars: ✭ 89 (-1.11%)
Mutual labels:  rails
Pluck all
A more efficient way to get data from database. Like #pluck method but return array of hashes instead.
Stars: ✭ 83 (-7.78%)
Mutual labels:  rails
Gorailsyourself
A suite of useful functions needed when porting/mixing Go/Rails code.
Stars: ✭ 89 (-1.11%)
Mutual labels:  rails
Errdo
A simple plugin to handle, log, and customize production errors in Rails applications
Stars: ✭ 88 (-2.22%)
Mutual labels:  rails
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (-4.44%)
Mutual labels:  rails
Rails or
Cleaner syntax for writing OR Query in Rails 5, 6. And also add #or support to Rails 3 and 4.
Stars: ✭ 86 (-4.44%)
Mutual labels:  rails

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"
    }
  ]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].