I'm new to this group and new to i18n as well. When confronted with
'no translation found' messages after adding a couple of translations,
I thought it would be nice to fall back to the :en locale when no
message is found, at least in production. The messages are returned
from the default exception handler, so I thought it would be easy
enough to make an extension there and implement my own exception
handler.
> def I18n.fallback(exception, locale, key, options)
> if MissingTranslationData === exception
> if not locale == :en
> return I18n.translate(key, options.merge(:locale => :en))
> else
> return exception.message
> end
> end
> # else
> raise exception
> end
Unfortunately, this won't work for active record messages, since they
rely on defaults being passed with the options and translate
deletes :default from the options before it is passed to the generated
exception (from i18n/backend/simple.rb):
> def translate(locale, key, options = {})
> ...
> options.delete(:default)
> ...
> if entry.nil?
> entry = default(locale, default, options)
> if entry.nil?
> raise(I18n::MissingTranslationData.new(locale, key,
> options))
> end
> end
> ...
> end
I think this is a bug, and the original options should be passed to
the exception, including :default. But, maybe I'm just overlooking
another easy way to solve this, or maybe there is a good
reason :default is being deleted?
(By the way, I'm aware of Globalize2 offering a fallback option and I
looked into the code, but it works by extending the backend and I
would rather keep it as simple as possible and don't need Globalize2
for anything else (at the moment).)
I managed to work around it for now, but any hints for a clean and
simple solution are more than welcome :)
--
Regards,
Eelco Lempsink
Thanks for the bump ;)
On 13 jan 2009, at 07:38, dasil003 wrote:
> After digging into the system I tend to agree with you. I don't see
> why they're deleting :default. But since your approach would require
> a core patch either way, I opted to just patch the core functionality
> of the simple backend. I forked Sven's repo and applied it there.
>
> http://github.com/dasil003/i18n/commit/46e04b6a609bed252251694647b3c6485b9021d4
I think your code would be fine in many situation, but it wouldn't
work for me. Two issues: 1) I like the 'missing translation'
exceptions, actually. When in development mode they pop out much more
than defaulting to another language, but that's a matter of taste;
more seriously, 2) I change the default locale, so falling back to
that will not work.
Glad to hear I'm not the only one with this problem. I hope this bug
is fixed soon.
--
Regards,
Eelco Lempsink