views:

27

answers:

1
# Transform the model name into a more humane format, using I18n. By default,
# it will underscore then humanize the class name
#
#   BlogPost.model_name.human # => "Blog post"
#
# Specify +options+ with additional translating options.

I found the above while digging through Rails trying to discover what exactly are the options for translating. I can find lots of references that say, use +options+ but can find no definitive method where these options are defined. What am I missing?

+2  A: 

The method you are talking about is ActiveRecord::Base#human

# Transform the model name into a more humane format, using I18n. By default,
# it will underscore then humanize the class name
#
#   BlogPost.model_name.human # => "Blog post"
#
# Specify +options+ with additional translating options.
def human(options={})
  return @human unless @klass.respond_to?(:lookup_ancestors) &&
                       @klass.respond_to?(:i18n_scope)

  defaults = @klass.lookup_ancestors.map do |klass|
    klass.model_name.underscore.to_sym
  end

  defaults << options.delete(:default) if options[:default]
  defaults << @human

  options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
  I18n.translate(defaults.shift, options)
end

that internally relies on I18n.translate. Thus options can be any option supported by the I18n method, including :scope, :default and more.

See http://guides.rubyonrails.org/i18n.html#looking-up-translations

The options argument also depends on the I18n backend you are currently using.

Simone Carletti
thanks, that sort of helped - sounds like the options aren't defined in the docs for a reason
brett