Take a look at its source, and everything will be clear to you:
# File activemodel/lib/active_model/translation.rb, line 45 def human_attribute_name(attribute, options = {}) defaults = [] parts = attribute.to_s.split(".", 2) attribute = parts.pop namespace = parts.pop 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
human_attribute_name great job of localizing the names of your attributes. Even if you are building an application that uses exclusively English, who knows, maybe your mailbox already has an email from your client about his plans to expand his business, and this extension will include countries where people write from right to left.
How it works
Suppose you have a Product model with a title attribute. Call human_attribute_name as follows:
Product.human_attribute_name 'title'
will call I18n.t (see the last line in the fragment above) with the following parameters:
I18.t 'activerecord.attributes.product.title', { :count=>1, :default=>[:"attributes.title", "Title"] }
In this way, you can provide translations that are specific to a model and specific to an attribute name. If you do not provide any of these translations, you will receive an English version of your attribute created using the humanize method.
source share