# Idea I18n.t(:not\_existing).presence == nil

**URL:** <https://www.ruby-forum.com/t/idea-i18n-t-not-existing-presence-nil/187070>\
**Category:** Rails I18n\
**Created:** [April 16, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/idea-i18n-t-not-existing-presence-nil/187070 "2010-04-16T15:15:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iain\_hecker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@iain\_hecker](https://www.ruby-forum.com/u/iain_hecker)\
**Post date:** [April 16, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/idea-i18n-t-not-existing-presence-nil/187070/1 "2010-04-16T15:15:07Z")

</div>

Hi guys,

I had an idea, just wondering what your thoughts on it are.

Short introduction: ActiveSupport 3 introduces the #presence method,  
which returns nil if #present? returns false, otherwise it returns  
self.

Can we define a singleton method on the strings that are returned when  
the translation is not found?

Like this:

> > foo = I18n.t(:foo)  
> > =\> “translation missing: en, foo”  
> > foo.present?  
> > =\> false  
> > foo.blank?  
> > =\> true  
> > foo.presence  
> > =\> nil

So, although it is a string (of some sort), it reacts as if its an  
empty string, since there is no translation.

I know this is kinda counter-intuative, but it allows you to write:

> > I18n.t(:foo).presence || I18n.t(:hello)  
> > =\> “World”

Implementation wise, we could define some extra methods on this string  
object, or we could make a MissingTranslation class that inherits from  
String.

So, what are your opinions on this? Does anyone know of a gem that  
already does something like this?

Cheers,  
Iain H.  
@iain\_nl / [iain (iain) · GitHub](http://github.com/iain) / [http://iain.nl](http://iain.nl)

---

<div class="post-metadata">

**Author:** ![iain\_hecker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c77e96/32.png) [@iain\_hecker](https://www.ruby-forum.com/u/iain_hecker)\
**Post date:** [April 16, 2010, 3:30pm UTC](https://www.ruby-forum.com/t/idea-i18n-t-not-existing-presence-nil/187070/2 "2010-04-16T15:30:56Z")

</div>

Hi Iain,

By default I18n.translate returns _“translation missing”_ string when  
translation is missing, so the _present?_ method always return true on  
that.  
This is default behaviour, which is fine.

You can do it another way - by defining exception handler returning  
nil on missing translation,  
if you really need it:

def your\_exception\_handler(exception, locale, key, options)  
if MissingTranslationData === exception  
# return exception.message  
return nil  
end  
raise exception  
end

I18n.exception\_handler = :your\_exception\_handler

Also, for such cases

> > > I18n.t(:foo).presence || I18n.t(:hello)  
> > > =\> “World”

you can always use default option I18n.t(:foo, :defult =\>  
I18n.t(:hello))

Regards,  
KK

2010/4/16 iain hecker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> String.
> 
> So, what are your opinions on this? Does anyone know of a gem that  
> already does something like this?
> 
> Cheers,  
> Iain H.  
> @iain\_nl / [iain (iain) · GitHub](http://github.com/iain) / [http://iain.nl](http://iain.nl)
> 
> –  
> You received this message because you are subscribed to the Google G.  
> “rails-i18n” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rails-i18n?hl=en](http://groups.google.com/group/rails-i18n?hl=en).
