Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Customize I18n::MissingTranslationData

#5782

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 (http://edgeguides.rubyonrails.org/i18n.html#using-different-excepti...)
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('.')
      else
        raise exception
    end
  end
end

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.
Titinux.

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

State: resolved
Milestone: 3.1
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. James Conroy-Finn
    James Conroy-Finn

    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
      end
    end
    
    I18n.exception_handler = :custom_handler
    

    …should do the trick.

    October 13th, 2010 @ 01:43 PM

  2. James Conroy-Finn
    James Conroy-Finn

    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.

    October 13th, 2010 @ 02:38 PM

  3. James Conroy-Finn
    James Conroy-Finn

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

    October 13th, 2010 @ 02:39 PM

  4. Aditya Sanghi
    Aditya Sanghi
    • 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.

    October 13th, 2010 @ 06:08 PM

  5. José Valim
    José Valim

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

    October 13th, 2010 @ 06:27 PM

  6. Jérémie Horhant (Titinux)
    Jérémie Horhant (Titinux)

    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.

    October 13th, 2010 @ 10:48 PM

  7. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:27 AM

  8. José Valim
    José Valim
    • 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.

    December 4th, 2010 @ 05:48 PM