Rails中定义的方法选项在哪里?

时间:2010-08-10 14:29:47

标签: ruby-on-rails internationalization

# 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.

我在挖掘Rails时发现了上述内容,试图找出确切的翻译选项。我可以找到许多引用,例如,使用+选项+但是找不到定义这些选项的确定方法。我错过了什么?

1 个答案:

答案 0 :(得分:2)

您正在谈论的方法是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

内部依赖I18n.translate。因此,options可以是I18n方法支持的任何选项,包括:scope:default等。

请参阅http://guides.rubyonrails.org/i18n.html#looking-up-translations

options参数还取决于您当前使用的I18n后端。