This project is archived and is in readonly mode.

#5782 ✓resolved
Jérémie Horhant (Titinux)

Customize I18n::MissingTranslationData

Reported by Jérémie Horhant (Titinux) | October 11th, 2010 @ 12:13 PM | in 3.1

In my project I need to customize the output of translate/t method
when there is no translation found. I've follow the rails guide about
I18n API section 6.2 (
and made a custom exception handler in an initializer file.

module I18n
  def custom_handler(exception, key, locale, options)
    case exception
      when I18n::MissingTranslationData
        I18n.normalize_keys(locale, key, options[:scope]).join('.')
        raise exception

I18n.exception_handler = :custom_handler

But this has no effect. I think I've found the code responsible for
this in actionpack-3.0.0/lib/action_view/helpers/translation_helper.rb
line 27. The option raise is forced to true and the translate methods
keeps the exception for itself with no possibility to replace the
exception handler nor customize the output.

In my opinion rails should not force raise option to true and make a
default exception handler. Thus it would be possible to declare a
custom handler to make whatever we wants in response to exceptions.

But may be I'm missing something...

Thanks in advance.

Comments and changes to this ticket

  • James Conroy-Finn

    James Conroy-Finn October 13th, 2010 @ 01:43 PM

    A custom exception handler needs to be defined on I18n itself. The code in the guide is wrong.

    module I18n
      def self.custom_handler(exception, key, locale, options)
        # custom error handling logic
    I18n.exception_handler = :custom_handler

    …should do the trick.

  • James Conroy-Finn

    James Conroy-Finn October 13th, 2010 @ 02:38 PM

    By the way, if you use the translate or t helper in any of your views your custom error handling will be bypassed because Rails specifies :raise => true as an option when calling I18n.translate.

    The result of this option is that an exception is raised before your custom error handling method is called. Hopefully this is something that will change in Rails soon.

  • James Conroy-Finn

    James Conroy-Finn October 13th, 2010 @ 02:39 PM

    Really wish I could edit updates! I didn't read the last couple of paragraphs in you OP. Apologies.

  • Aditya Sanghi

    Aditya Sanghi October 13th, 2010 @ 06:08 PM

    • Assigned user set to “José Valim”
    • Importance changed from “” to “Low”

    @james, yes there are a few limitations on Lighthouse but apparently ENTP are working on a major update.

    I know what you mean, with rails hardcoding a :raise => true preventing user handling of lookup errors.

    Assigning to Jose for his input on this.

    Ideally you could submit a patch with tests which could help speed up the process.

  • José Valim

    José Valim October 13th, 2010 @ 06:27 PM

    James, do you think you can provide a patch to solve this issue?

  • Jérémie Horhant (Titinux)

    Jérémie Horhant (Titinux) October 13th, 2010 @ 10:48 PM

    I continued my investigation and I found a puzzling behavior of the translate/t methods.

    These three implementations have different behaviors this is puzzling. The first one is just calling the third one but the second is doing a lot of work. The second is not perfect (not customizable and restrictive, cf the beginning of this ticket) but it doing some cool things (translation naming convention ending with _html or .html is really a good idea).

    I know that I am just complaining but I'm new to rails and I'm afraid of not being able to produce a clean patch with tests. Eventually I could fix the documentation when changes have been made.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:27 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • José Valim

    José Valim December 4th, 2010 @ 05:48 PM

    • State changed from “new” to “resolved”
    • Milestone set to 3.1

    This has been changed and fixed in Rails master using the new I18n 0.5.0 API.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>