All Projects → RST-J → Human_attribute_values

RST-J / Human_attribute_values

Licence: mit
Translation of attribute values in rails

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Human attribute values

I18n Debug
Ever wondered which translations are being looked up by Rails, a gem, or simply your app? Wonder no more!
Stars: ✭ 143 (+1942.86%)
Mutual labels:  translation, rails
Spree i18n
I18n translation files for Spree Commerce.
Stars: ✭ 338 (+4728.57%)
Mutual labels:  translation, rails
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+9100%)
Mutual labels:  translation, rails
Libraries.io
📚 The Open Source Discovery Service
Stars: ✭ 903 (+12800%)
Mutual labels:  rails
Rails Api And Angularjs
Integration between rails and angularjs which includes rspec tests.
Stars: ✭ 22 (+214.29%)
Mutual labels:  rails
Rails Routes
Enable config/routes/*.rb on your Rails application.
Stars: ✭ 24 (+242.86%)
Mutual labels:  rails
Fanyi
A 🇨🇳 and 🇺🇸 translate tool in your command line.
Stars: ✭ 940 (+13328.57%)
Mutual labels:  translation
Papago
PAPAGO translate API with Python
Stars: ✭ 18 (+157.14%)
Mutual labels:  translation
Dotenv sekrets
Seamlessly encrypt/decrypt/edit your rails Dotenv files with the help of the Sekrets gem
Stars: ✭ 25 (+257.14%)
Mutual labels:  rails
Best Practices Badge
🏆Core Infrastructure Initiative Best Practices Badge
Stars: ✭ 928 (+13157.14%)
Mutual labels:  rails
Milog
Milog 是一基于 Ruby on Rails 的个人博客网站
Stars: ✭ 24 (+242.86%)
Mutual labels:  rails
Foundation Datetimepicker Rails
foundation-datetimepicker-rails
Stars: ✭ 22 (+214.29%)
Mutual labels:  rails
Activejob Scheduler
A background job scheduler for any queue backend
Stars: ✭ 24 (+242.86%)
Mutual labels:  rails
Redis Rails
Redis stores for Ruby on Rails
Stars: ✭ 904 (+12814.29%)
Mutual labels:  rails
Rails Erd D3
Ruby gem for creating entity–relationship diagram with D3.js for your Rails application 📊
Stars: ✭ 25 (+257.14%)
Mutual labels:  rails
Api schema
DSL for describing APIs and generate swagger json
Stars: ✭ 18 (+157.14%)
Mutual labels:  rails
Muscle man
💪🏻It is a chatbot which can receive your practice record!
Stars: ✭ 25 (+257.14%)
Mutual labels:  rails
Satakieli
Satakieli is a i18n library that provides identical API for ClojureScript and Clojure programmers. Localized messages can be written using ICU MessageFormat syntax.
Stars: ✭ 24 (+242.86%)
Mutual labels:  translation
Datagrid
Gem to create tables grids with sortable columns and filters
Stars: ✭ 921 (+13057.14%)
Mutual labels:  rails
Rails5 Api Starter
RESTful API Starter Kit based on Rails 5
Stars: ✭ 24 (+242.86%)
Mutual labels:  rails

human_attribute_values

Build Status Gem Version

human_attribute_values is a Rails plugin which provides translation for model attribute values using the Rails I18n API (analogously to human_attribute_name).

Installation

gem install human_attribute_values

Supported versions

  • Rails: >= 4.2.10
  • Ruby: MRI >= 2.3.0

Usage

The gem defines human_attribute_value as instance and class method on ActiveRecord::Base and ActiveModel::Model. To translate a value it uses the I18n API. The translations are looked up from the current locale file under the key 'activerecord.values.model_name.attribute_name.value' respectively 'activemodel.values.model_name.attribute_name.value' by default.

Locale:

en:
  activemodel:
    values:
      file_model:
        content_type:
          application/pdf: PDF
          application/vnd_openxmlformats-officedocument_spreadsheetml_sheet: Excel/Calc
  activerecord:
    values:
      schroedinger:
        cat_status:
          dead_and_alive: The box is still closed.
          alive: You opened the box and kitty purrs.

Translations:

# For ActiveRecord

class Schroedinger < ActiveRecord::Base
  enum cat_status: {dead_and_alive: 0, alive: 1, dead: 2}
end

# Translation on instances by passing the attribute to be translated
Schroedinger.new(cat_status: :dead_and_alive).human_attribute_value(:cat_status)
 => "The box is still closed."

# Translation on the model class by passing attribute and value
Schroedinger.human_attribute_value(:cat_status, :alive)
 => "You opened the box and kitty purs."

# If there is no translation specified in the locale, the stringified value is returned
Schroedinger.human_attribute_value(:cat_status, :dead)
 => "dead"

# For ActiveModel (the same except the lookup namespace)
class FileModel
  include ActiveModel::Model

  attr_accessor :content_type
end

file = FileModel.new(content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
file.human_attribute_value(:content_type)
 => "Excel/Calc"

FileModel.human_attribute_value(:content_type, 'application/pdf')
 => "PDF"

FileModel.human_attribute_value(:content_type, 'text/plain')
 => "text/plain"

Boolean values

To translate boolean values, the key must be specified as string:

en:
  activerecord:
    values:
      user:
        active:
          'true': active
          'false': inactive

Numeric values

If there is a reason to translate numbers, their keys must also be given as strings. For decimal values the dot is replaced by an underscore for the lookup.

en:
  activerecord:
    values:
      magic_number:
        value:
          '3_14': Pi
          '42': 'the answer to life, the universe and everything'

Strings and Symbols

Starting with version 1.2.0, dots in strings and symbols (actually in all values) are also replaced by an underscore for the lookup (before this was only done for numbers).

Child classes

For models with single table inheritance (STI), the lookup will start with the translations for the class and go up through the class hierarchy and use the translation for the closest parent if there is any.

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].