我有這樣的自定義異常處理程序:的I18n定義異常處理程序不調用`t`,只能用`I18n.translate`
module I18n
class MissingTanslationsCollectorExceptionHandler < I18n::ExceptionHandler
# Handles exceptions from I18n
def call(exception, locale, key, options)
if exception.is_a?(I18n::MissingTranslation)
binding.pry
missing_translations << I18n.normalize_keys(locale, key, options[:scope])
super
end
end
end
end
我給你這樣的:
I18n.exception_handler = I18n::MissingTanslationsCollectorExceptionHandler.new
當使用控制檯,它似乎工作:
$ rails c
Loading development environment (Rails 4.2.0)
[1] base » I18n.exception_handler
=> #<I18n::MissingTanslationsCollectorExceptionHandler:0x00000101c121e8>
[2] base » I18n.translate 'unknown'
From: /Users/josh/Documents/Work/MuheimWebdesign/base/src/lib/missing_i18n_exception_handler.rb @ line 11 I18n::MissingTanslationsCollectorExceptionHandler#call:
10: if exception.is_a?(I18n::MissingTranslation)
=> 11: binding.pry
12: missing_translations << I18n.normalize_keys(locale, key, options[:scope])
[1] base(#<I18n::MissingTanslationsCollectorExceptionHandler>) » c
=> "translation missing: en.unknown"
但啓動服務器,並擊中缺少的翻譯時,不調用,binding.pry
。只有在執行I18n.translate
未知時,纔會被調用。爲什麼?
也許它與該功能規格使用水豚自己的進程中運行的事實呢?
更新
這裏是有問題的Rails應用程序: https://github.com/jmuheim/base/tree/features/custom-i18n-exception-handler
我已經在這裏添加了自定義的國際化異常處理程序: https://github.com/jmuheim/base/commit/f2aff30046c7a9f38c4a1faed0953e474099120c
而且我已經添加代碼,應該表現出問題在這裏: https://github.com/jmuheim/base/commit/af484b6b96f41194043e0ad0668a5c288d4a0af3
只需轉到root_path,然後觸發它(一次,不是兩次!)。
你把'class MissingTanslationsCollectorExceptionHandler'放在哪裏? – 2015-04-06 09:31:07