Rails I18n translate with empty string – bug?

Normally, it's pretty easy in Rails to take advantage of the simple i18n tools provided.  You just write I18n.t("some_key") and it'll translate that into whichever language.

Today, however, I discovered what seems like a problem.  If you do I18n.t(""), it returns a hash of all possible translations!  And if you give it a nil, it returns an error because that's not a key in the DB.

The first behavior doesn't sound correct at all, and the second seems dubious as well.  I'm thinking about fixing it like so:
 

  def translate(key, options = {})
    return nil if key.nil?
    return "" if key == ""
    locale = options.delete(:locale) || I18n.locale
    backend.translate(locale, key, options)
  rescue I18n::ArgumentError => e
    raise e if options[:raise]
    send(@@exception_handler, e, locale, key, options)
  end

Is there any reason not to do this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s