我创建了一个虚拟rails 4.1.5应用程序,以便了解如何使用不同的异常处理程序,如here所述。我只将此文件添加到脚手架生成的应用程序中:
#config/initializers/i18n.rb
module I18n
class JustRaiseExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
puts ">>>>>>>>> #{exception.class}"
super
end
end
end
I18n.exception_handler = I18n::JustRaiseExceptionHandler.new
在我的控制台中:
Loading development environment (Rails 4.1.5)
irb(main):001:0> I18n.backend.store_translations :en, thanks: 'Thanks %{name}!'; nil
=> nil
irb(main):002:0> I18n.translate :thanks, name: 'john'
=> "Thanks john!"
irb(main):003:0> I18n.translate :foo, name: 'john'
>>>>>>>>> I18n::MissingTranslation
=> "translation missing: en.foo"
irb(main):004:0> I18n.translate :thanks, foo: 'john'
I18n::MissingInterpolationArgument: missing interpolation argument :name in "Thanks %{name}!" ({:foo=>"john"} given)
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/config.rb:92:in `block in missing_interpolation_argument_handler'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/interpolate/ruby.rb:29:in `call'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/interpolate/ruby.rb:29:in `block in interpolate_hash'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/interpolate/ruby.rb:21:in `gsub'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/interpolate/ruby.rb:21:in `interpolate_hash'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/interpolate/ruby.rb:17:in `interpolate'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/backend/base.rb:152:in `interpolate'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n/backend/base.rb:41:in `translate'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n.rb:158:in `block in translate'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n.rb:154:in `catch'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/i18n-0.7.0/lib/i18n.rb:154:in `translate'
from (irb):4
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
from /Users/masciugo/dev/ciccio/bin/rails:8:in `<top (required)>'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/masciugo/.rbenv/versions/2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'irb(main):005:0>
为什么在I18n :: MissingInterpolationArgument的情况下不会调用我的自定义处理程序?