I have this custom exception handler:
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
I assign it like this:
I18n.exception_handler = I18n::MissingTanslationsCollectorExceptionHandler.new
When using the console, it seems to work:
$ 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"
But when starting the server and hitting a missing translation with t 'unknown', binding.pry isn't called. Only when doing a I18n.translateunknown`, it is called. Why?
Maybe it has to do with the fact that the feature specs are run inside their own process using Capybara?
Update
Here is the Rails app in question: https://github.com/jmuheim/base/tree/features/custom-i18n-exception-handler
I have added the custom i18n exception handler here: https://github.com/jmuheim/base/commit/f2aff30046c7a9f38c4a1faed0953e474099120c
And I have added code which should demonstrate the issue here: https://github.com/jmuheim/base/commit/af484b6b96f41194043e0ad0668a5c288d4a0af3
Simply go the to root_path, then it should be triggered (once, not twice!).
This happens because ActionView's t isn't a simple alias to I18n.translate. Among other things, it converts any MissingTranslation exceptions to spans in your HTML.
See the code for details.
I wanted exceptions to bubble up, but I didn't want to have to specify rescue_format: true every time I called t in my views, so I overrode Rails's helper to always pass rescue_format: true as an option.
Another thing to note: the translation helper has changed a bit from one Rails version to the next, so make sure you read the actual code for your version to see what to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With