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

Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to