diff options
author | xyctka@gmail.com <xyctka@gmail.com> | 2012-04-06 10:01:34 +0100 |
---|---|---|
committer | xyctka@gmail.com <xyctka@gmail.com> | 2012-04-06 10:01:34 +0100 |
commit | 74fa0d3db0c364f91d8a9a8ab0741d5787531890 (patch) | |
tree | 4d6e82ad223a9c11c0e68d7f606c1b13627c7ba0 /guides/source/i18n.textile | |
parent | b11113f924d2eb2acbe836954d17a02163f45275 (diff) | |
download | rails-74fa0d3db0c364f91d8a9a8ab0741d5787531890.tar.gz rails-74fa0d3db0c364f91d8a9a8ab0741d5787531890.tar.bz2 rails-74fa0d3db0c364f91d8a9a8ab0741d5787531890.zip |
Update Custom Exception Handler section of Internationalization guide
The previous example with just_raise_that_exception method
stopped working because MissingTranslation is not an instance of
Exception class any more, but has a +to_exception+ method.
Also the cleaner way is to re-raise only the desired exception, passing
everything else to the default ExceptionHandler.
Additionally this re-raising conflicts with Pluralization backend thus
we have to add a check that certain missing translation keys should
be ignored allowing the backend to fall back to the default
pluralization rule.
Diffstat (limited to 'guides/source/i18n.textile')
-rw-r--r-- | guides/source/i18n.textile | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/guides/source/i18n.textile b/guides/source/i18n.textile index 320f1e9d20..89d67e1f92 100644 --- a/guides/source/i18n.textile +++ b/guides/source/i18n.textile @@ -866,19 +866,35 @@ The I18n API will catch all of these exceptions when they are thrown in the back The reason for this is that during development you'd usually want your views to still render even though a translation is missing. -In other contexts you might want to change this behaviour, though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module: +In other contexts you might want to change this behaviour, though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module or a class with +#call+ method: <ruby> module I18n - def self.just_raise_that_exception(*args) - raise args.first + class JustRaiseHandler < ExceptionHandler + def call(exception, locale, key, options) + if exception.is_a?(MissingTranslation) + raise exception.to_exception + else + super + end + end end end -I18n.exception_handler = :just_raise_that_exception +I18n.exception_handler = I18n::JustRaiseHandler.new </ruby> -This would re-raise all caught exceptions including +MissingTranslationData+. +This would re-raise only the +MissingTranslationData+ exception, passing all other input to the default exception handler. + +However, if you are using +I18n::Backend::Pluralization+ this handler will also raise +I18n::MissingTranslationData: translation missing: en.i18n.plural.rule+ exception that should normally be ignored to fall back to the default pluralization rule for English locale. To avoid this you may use additional check for translation key: + +<ruby> +if exception.is_a?(MissingTranslation) && key.to_s != 'i18n.plural.rule' + raise exception.to_exception +else + super +end +</ruby> Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method +#t+ (as well as +#translate+). When a +MissingTranslationData+ exception occurs in this context, the helper wraps the message into a span with the CSS class +translation_missing+. |