Module: ActiveModel::Translation

### Home Classes Methods

### In Files

- lib/active_model/translation.rb

### Methods

- #human_attribute_name
- #i18n_scope
- #lookup_ancestors

### Included Modules

- ActiveModel::Naming

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::Translation

## Active Model Translation

Provides integration between your object and the Rails internationalization (i18n) framework.

A minimal implementation could be:

    class TranslatedPerson
      extend ActiveModel::Translation
    end

    TranslatedPerson.human_attribute_name('my_attribute')
    # => "My attribute"

This also provides the required class methods for hooking into the Rails internationalization API, including being able to define a class based `i18n_scope` and `lookup_ancestors` to find translations in parent classes.

### Public Instance Methods

human_attribute_name(attribute, options = {}) click to toggle source

Transforms attribute names into a more human format, such as "First name" instead of "first_name".

    Person.human_attribute_name("first_name") # => "First name"

Specify `options` with additional translating options.

    # File lib/active_model/translation.rb, line 45
    def human_attribute_name(attribute, options = {})
      defaults  = []
      parts     = attribute.to_s.split(".")
      attribute = parts.pop
      namespace = parts.join("/") unless parts.empty?

      if namespace
        lookup_ancestors.each do |klass|
          defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
        end
        defaults << :"#{self.i18n_scope}.attributes.#{namespace}.#{attribute}"
      else
        lookup_ancestors.each do |klass|
          defaults << :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
        end
      end

      defaults << :"attributes.#{attribute}"
      defaults << options.delete(:default) if options[:default]
      defaults << attribute.humanize

      options.reverse_merge! :count => 1, :default => defaults
      I18n.translate(defaults.shift, options)
    end

i18n_scope() click to toggle source

Returns the `i18n_scope` for the class. Overwrite if you want custom lookup.

    # File lib/active_model/translation.rb, line 27
    def i18n_scope
      :activemodel
    end

lookup_ancestors() click to toggle source

When localizing a string, it goes through the lookup returned by this method, which is used in ActiveModel::Name#human, ActiveModel::Errors#full_messages and ActiveModel::Translation#human_attribute_name.

    # File lib/active_model/translation.rb, line 35
    def lookup_ancestors
      self.ancestors.select { |x| x.respond_to?(:model_name) }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.