2010-08-10 54 views
0
# 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試圖發現翻譯的選項時,我發現了上述內容。我可以找到很多參考文獻說,使用+選項+,但可以找到定義這些選項的定義方法。我錯過了什麼?Rails中定義的方法選項在哪裏?

回答

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。

+0

謝謝,這種幫助 - 聽起來像選項沒有在文檔中定義的原因 – Brett 2010-08-12 09:13:07