2009-10-20 20:20:01 -04:00
|
|
|
module ActiveModel
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model \Translation
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-06-14 05:26:51 -04:00
|
|
|
# Provides integration between your object and the Rails internationalization
|
|
|
|
# (i18n) framework.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-18 02:13:49 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-18 02:13:49 -05:00
|
|
|
# class TranslatedPerson
|
|
|
|
# extend ActiveModel::Translation
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-06-11 06:15:34 -04:00
|
|
|
# TranslatedPerson.human_attribute_name('my_attribute')
|
2010-07-29 20:30:04 -04:00
|
|
|
# # => "My attribute"
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-18 02:13:49 -05:00
|
|
|
# This also provides the required class methods for hooking into the
|
|
|
|
# Rails internationalization API, including being able to define a
|
2011-04-26 22:26:10 -04:00
|
|
|
# class based +i18n_scope+ and +lookup_ancestors+ to find translations in
|
2010-01-18 02:13:49 -05:00
|
|
|
# parent classes.
|
2009-10-20 20:20:01 -04:00
|
|
|
module Translation
|
|
|
|
include ActiveModel::Naming
|
|
|
|
|
2011-04-26 22:26:10 -04:00
|
|
|
# Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup.
|
2009-10-20 20:20:01 -04:00
|
|
|
def i18n_scope
|
|
|
|
:activemodel
|
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# When localizing a string, it goes through the lookup returned by this
|
2010-06-14 05:26:51 -04:00
|
|
|
# method, which is used in ActiveModel::Name#human,
|
2010-08-14 01:13:00 -04:00
|
|
|
# ActiveModel::Errors#full_messages and
|
2009-10-20 20:20:01 -04:00
|
|
|
# ActiveModel::Translation#human_attribute_name.
|
|
|
|
def lookup_ancestors
|
2016-08-07 19:05:28 -04:00
|
|
|
ancestors.select { |x| x.respond_to?(:model_name) }
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
2010-06-14 05:26:51 -04:00
|
|
|
# Transforms attribute names into a more human format, such as "First name"
|
|
|
|
# instead of "first_name".
|
2009-10-20 20:20:01 -04:00
|
|
|
#
|
|
|
|
# Person.human_attribute_name("first_name") # => "First name"
|
|
|
|
#
|
|
|
|
# Specify +options+ with additional translating options.
|
|
|
|
def human_attribute_name(attribute, options = {})
|
2013-05-01 20:10:06 -04:00
|
|
|
options = { count: 1 }.merge!(options)
|
2012-04-14 05:00:54 -04:00
|
|
|
parts = attribute.to_s.split(".")
|
2011-12-05 09:39:41 -05:00
|
|
|
attribute = parts.pop
|
2012-04-14 05:00:54 -04:00
|
|
|
namespace = parts.join("/") unless parts.empty?
|
2012-04-28 11:06:51 -04:00
|
|
|
attributes_scope = "#{self.i18n_scope}.attributes"
|
2011-12-05 09:39:41 -05:00
|
|
|
|
|
|
|
if namespace
|
2012-04-28 11:06:51 -04:00
|
|
|
defaults = lookup_ancestors.map do |klass|
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
|
2011-12-05 09:39:41 -05:00
|
|
|
end
|
2012-04-28 11:06:51 -04:00
|
|
|
defaults << :"#{attributes_scope}.#{namespace}.#{attribute}"
|
2011-12-05 09:39:41 -05:00
|
|
|
else
|
2012-04-28 11:06:51 -04:00
|
|
|
defaults = lookup_ancestors.map do |klass|
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}"
|
2011-12-05 08:57:47 -05:00
|
|
|
end
|
2011-06-11 11:21:22 -04:00
|
|
|
end
|
2009-10-20 20:20:01 -04:00
|
|
|
|
2010-01-06 20:45:50 -05:00
|
|
|
defaults << :"attributes.#{attribute}"
|
2009-10-20 20:20:01 -04:00
|
|
|
defaults << options.delete(:default) if options[:default]
|
2011-12-05 09:39:41 -05:00
|
|
|
defaults << attribute.humanize
|
2009-10-20 20:20:01 -04:00
|
|
|
|
2012-04-28 10:23:29 -04:00
|
|
|
options[:default] = defaults
|
2009-10-20 20:20:01 -04:00
|
|
|
I18n.translate(defaults.shift, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|