Question about i18n / locale fallback

78 views
Skip to first unread message

Eelco Lempsink

unread,
Jan 3, 2009, 2:24:47 PM1/3/09
to rails...@googlegroups.com
Hi there,

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

PGP.sig

dasil003

unread,
Jan 12, 2009, 8:47:05 PM1/12/09
to rails-i18n
I'm also new to i18n and in the early stages of implementing a partial
localization of my app to 5 languages. I haven't gotten this far yet,
but I anticipated this issue after reading the usual blog posts. Just
bumping the thread in case someone has something to add, I'll be back
soon...
>  PGP.sig
> < 1KViewDownload

dasil003

unread,
Jan 13, 2009, 1:38:46 AM1/13/09
to rails-i18n
Eelco,

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

Maybe this will be useful to others. I think it would even be cool to
integrate, but I can see how it would have to be optional for those
cases where translations must be complete. What do you think i18n
guys?

On Jan 3, 11:24 am, Eelco Lempsink <ee...@lempsink.nl> wrote:
>  PGP.sig
> < 1KViewDownload

Kip

unread,
Jan 13, 2009, 10:24:20 PM1/13/09
to rails-i18n
I've just uploaded a plugin to do something similar to this, located
at http://github.com/kipcole9/rails-i18n-translation-inheritance-helper/tree/master
in case it helps.

Cheers, --Kip

On Jan 13, 5:38 pm, dasil003 <gabrie...@gmail.com> wrote:
> Eelco,
>
> 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/46e04b6a609bed252251694647b3c6...

Eelco Lempsink

unread,
Jan 14, 2009, 3:27:42 PM1/14/09
to rails...@googlegroups.com
Gabriel,

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

PGP.sig
Reply all
Reply to author
Forward
0 new messages