Giter Site home page Giter Site logo

koerneml / human_attribute_values Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rst-j/human_attribute_values

0.0 0.0 0.0 89 KB

Translation of attribute values in rails

License: MIT License

Ruby 86.92% JavaScript 1.45% CSS 1.32% HTML 10.30%

human_attribute_values's Introduction

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.

human_attribute_values's People

Contributors

rst-j 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.