2

我们使用自己的异常处理程序来处理丢失的翻译:

# Logs missing I18n translations to `missing_translations.log`.
I18n.exception_handler = lambda do |exception, locale, key, options|
  e = exception.respond_to?(:to_exception) ? exception.to_exception : exception
  # log rotate, because it's not important enough to keep
  @log ||= Logger.new(File.join(Rails.root, 'log', 'missing_translations.log'),2,5*1024*1024)
  if e.is_a? I18n::MissingTranslationData
    @log.info "#{locale}.#{key}"
  else
    raise e
  end
  exception.to_s
end

当我们的测试套件运行时,记录器的内容被保存到一个文件中,我们可以在其中查看是否缺少翻译。这非常有用,但通常并不清楚在哪里调用了丢失的键,因此可以修复它(例如在翻译通用键时)。遗憾的是我没有找到获取文件名和行号的方法,但我确定有解决方案吗?

我尝试了backtrace有关异常的方法,但这并没有提供更多信息。

[6] pry(main)> e
=> #<I18n::MissingTranslationData: translation missing: avatar.devise.failure.already_authenticated>
[7] pry(main)> e.to_exception
=> #<I18n::MissingTranslationData: translation missing: avatar.devise.failure.already_authenticated>
[8] pry(main)> e.to_exception.backtrace
=> nil
[9] pry(main)> e
=> #<I18n::MissingTranslationData: translation missing: avatar.devise.failure.already_authenticated>
[10] pry(main)> e.backtrace
=> nil

我也发现了这个,但这似乎已经过时了。

非常感谢您的帮助

** 更新 1 **

使用caller我得到以下堆栈跟踪:

pry(main)> caller(4)
=> ["/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:278:in `re'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:254:in `rep'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:234:in `block (3 levels) in repl'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:232:in `loop'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:232:in `block (2 levels) in repl'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:231:in `catch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:231:in `block in repl'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:230:in `catch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_instance.rb:230:in `repl'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/pry_class.rb:170:in `start'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-debugger-0.2.1/lib/pry-debugger/pry_ext.rb:15:in `block in start_with_pry_debugger'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-debugger-0.2.1/lib/pry-debugger/processor.rb:18:in `block in run'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-debugger-0.2.1/lib/pry-debugger/processor.rb:17:in `catch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-debugger-0.2.1/lib/pry-debugger/processor.rb:17:in `run'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-debugger-0.2.1/lib/pry-debugger/pry_ext.rb:14:in `start_with_pry_debugger'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/pry-0.9.12/lib/pry/core_extensions.rb:43:in `pry'",
 "/Users/josh/Documents/Work/Sientia/iq/config/initializers/i18n.rb:8:in `block in <top (required)>'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/i18n-0.6.1/lib/i18n.rb:297:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/i18n-0.6.1/lib/i18n.rb:297:in `handle_exception'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/i18n-0.6.1/lib/i18n.rb:159:in `translate'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/devise-2.1.2/app/controllers/devise_controller.rb:130:in `require_no_authentication'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:418:in `_run__818949352997764593__process_action__4268511465162580532__callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/abstract_controller/callbacks.rb:17:in `process_action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal/rescue.rb:29:in `process_action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal/instrumentation.rb:29:in `process_action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/abstract_controller/base.rb:121:in `process'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/abstract_controller/rendering.rb:45:in `process'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal.rb:203:in `dispatch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_controller/metal.rb:246:in `block in action'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:73:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:36:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/routing/mapper.rb:42:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:601:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-contrib-1.1.0/lib/rack/contrib/response_headers.rb:17:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/meta_request-0.2.1/lib/meta_request/middlewares/headers.rb:16:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/meta_request-0.2.1/lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/request_store-1.0.5/lib/request_store/middleware.rb:9:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/mongoid-3.0.22/lib/rack/mongoid/middleware/identity_map.rb:34:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/mongoid-3.0.22/lib/mongoid/unit_of_work.rb:39:in `unit_of_work'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/mongoid-3.0.22/lib/rack/mongoid/middleware/identity_map.rb:34:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/sass-3.2.3/lib/sass/plugin/rack.rb:54:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/etag.rb:23:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/conditionalget.rb:25:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/head.rb:14:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/params_parser.rb:21:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/flash.rb:242:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/cookies.rb:341:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `_run__2503506183694590348__call__3723261410109716825__callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:27:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:65:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/runtime.rb:17:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/lock.rb:15:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/railties-3.2.12/lib/rails/engine.rb:479:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/railties-3.2.12/lib/rails/application.rb:223:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/builder.rb:134:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/urlmap.rb:64:in `block in call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/urlmap.rb:49:in `each'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/urlmap.rb:49:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/capybara-1.1.4/lib/capybara/server.rb:17:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/connection.rb:81:in `block in pre_process'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/connection.rb:79:in `catch'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/connection.rb:79:in `pre_process'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/connection.rb:54:in `process'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/backends/base.rb:63:in `start'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/thin-1.5.0/lib/thin/server.rb:159:in `start'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/rack-1.4.5/lib/rack/handler/thin.rb:13:in `run'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/capybara-1.1.4/lib/capybara.rb:177:in `run_default_server'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/capybara-1.1.4/lib/capybara.rb:236:in `block (2 levels) in <top (required)>'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/capybara-1.1.4/lib/capybara/server.rb:65:in `call'",
 "/Users/josh/.rvm/gems/ruby-1.9.3-p327-falcon@iq/gems/capybara-1.1.4/lib/capybara/server.rb:65:in `block in boot'"]
[2] pry(main)> 

可悲的是,我仍然无法真正看到翻译在哪里使用。有人看到吗?

4

1 回答 1

1

Kernel#caller返回当前的执行堆栈,所以这样的事情应该可以工作:

I18n.exception_handler = lambda do |exception, locale, key, options|
  # ...
  if e.is_a? I18n::MissingTranslationData
    stack = caller(4) # omit 4 levels up to 'translate' call
    log.info "#{locale}.#{key}"
    log.info stack.join("\n")
    # ...
  end
  # ...
end
于 2013-02-27T13:19:01.127 回答