module ActiveModel::Translation
def human_attribute_name(attribute, options = {})
Person.human_attribute_name("first_name") # => "First name"
instead of "first_name".
Transforms attribute names into a more human format, such as "First name"
def human_attribute_name(attribute, options = {}) options = { count: 1 }.merge!(options) parts = attribute.to_s.split(".") attribute = parts.pop namespace = parts.join("/") unless parts.empty? attributes_scope = "#{i18n_scope}.attributes" if namespace defaults = lookup_ancestors.map do |klass| :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}" end defaults << :"#{attributes_scope}.#{namespace}.#{attribute}" else defaults = lookup_ancestors.map do |klass| :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}" end end defaults << :"attributes.#{attribute}" defaults << options.delete(:default) if options[:default] defaults << attribute.humanize options[:default] = defaults I18n.translate(defaults.shift, **options) end
def i18n_scope
def i18n_scope :activemodel end
def lookup_ancestors
ActiveModel::Errors#full_messages and
method, which is used in ActiveModel::Name#human,
When localizing a string, it goes through the lookup returned by this
def lookup_ancestors ancestors.select { |x| x.respond_to?(:model_name) } end