Rails: How to raise I18n translation is missing exceptions in the testing environment

rails-i18n
rails missing i18n
rails i18n missing translation
translation missing rails
activerecord errors i18n
i18n localize
ruby i18n
rspec translation missing

I want Rails to raise an exception when an I18n translation is missing in the testing environment (instead of rendering text 'translation missing'). Is there a simple way to achieve this?


To raise exceptions, you can define a class to handle localization errors.

class TestExceptionLocalizationHandler
  def call(exception, locale, key, options)
    raise exception.to_exception
  end
end

Then you attach it to the desired test cases with

I18n.exception_handler = TestExceptionLocalizationHandler.new

This way you get exceptions raised. I don't know how to raise failures (with flunk) to get better results.

Foolproof I18n Setup in Rails, I recommend raising exceptions in the test and development environments, so that you can find missing translations by running tests and by  Most versions of Rails include a way to raise exceptions when a translation is missing. Raising an exception ensures that all of your calls to t() are using your copy, instead of using a default string. I recommend raising exceptions in the test and development environments, so that you can find missing translations by running tests and by browsing around on localhost.


As of Rails 4.1.0, there's now a better solution than the 4 year-old answers to this question: add the following line to your config file:

config.action_view.raise_on_missing_translations = true

I like to set this in the test environment only, but you might also want to set it in development. I would strongly advise against setting it to true in production.

Raise when there's a I18n translation missing, I want Rails to raise an exception when an I18n translation is missing in the testing environment (instead of rendering text 'translation missing'). Is there a simple  @firedev to override handler you should use t('.missing', raise: true).Then will be raised exception, which you should handle/catch yourselves. @NZKoz, I think there is a way to support I18n.exception_handler, should we add patch to return supporting of it, or should this feature be removed, and updated docs that I18n.exception_handler is not working anymore?


Rails 4.1+

To raise i18n translation missing exceptions you need two things:

1) An initializer config/initializers/i18n_force_exceptions.rb:

module I18n
  class ForceMissingTranslationsHandler < ExceptionHandler
    def call(exception, locale, key, options)
      if Rails.env.test?
        raise exception.to_exception
      else
        super
      end
    end
  end
end

I18n.exception_handler = I18n::ForceMissingTranslationsHandler.new

2) A config setting in config/environments/test.rb (and other environments as needed):

config.action_view.raise_on_missing_translations = true

Note: The config setting is needed in addition to the exception handler because rails wraps calls to I18n.translate in it's view and helpers preventing exceptions from triggering.

Testing i18n in Ruby on Rails: Rooting Out Missing Translations , The attached initializer makes I18n just raise any exception (in a development or test environment). Long-term care for Ruby on Rails applications. Once an  i18n-tasks . i18n-tasks helps you find and manage missing and unused translations. This gem analyses code statically for key usages, such as I18n.t('some.key'), in order to:


Or you can just add those lines to your config/test.rb

  config.action_view.raise_on_missing_translations = true
  config.i18n.exception_handler = Proc.new { |exception| raise exception.to_exception }

Rails Internationalization (I18n) API, config/environments/test.rb # config/environments/development.rb For information regarding raising exceptions on missing translations in  Rails Internationalization (I18n) APIThe Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.The process of "internationalization" usually means


I've created this initializer to raise an exception - args are passed so you will know which i18n key is missing!

# only for test
if Rails.env.test?

  # raises exception when there is a wrong/no i18n key
  module I18n
    class JustRaiseExceptionHandler < ExceptionHandler
      def call(exception, locale, key, options)
        if exception.is_a?(MissingTranslation)
          raise exception.to_exception
        else
          super
        end
      end
    end
  end

  I18n.exception_handler = I18n::JustRaiseExceptionHandler.new

end

Source

Missing translations keys, This method will re-raise all exceptions except for catch missing translations during automated tests easily. 19 Rails: How to raise I18n translation is missing exceptions in the testing environment 16 Is there a way to know the current rake task? 14 Best practices to handle routes for STI subclasses in rails


ruby-i18n/i18n, config/initializers/i18n.rb if Rails.env.in?(%(development test)) I18n.​exception_handler = Proc.new do |exception, *_| raise exception. To do so, the helper forces I18n#translate to raise exceptions no matter what exception handler is defined by setting the :raise option: I18n.t :foo, :raise => true # always re-raises exceptions from the backend


Using I18n in capybara feature specs, Better error message when `I18n::TranslationMissingData` is rescued. I18n.t('​some_key', wrong_key: 'testing') => "translation data missing: en.some_key" the default string that is returned when a I18n::MissingTranslationData is raised. @tgxworld It looks like maybe somewhere in your environment,  They will raise I18n::MissingTranslationData on a missing translation instead of printing a string like translation missing: de.custom.failure. To turn on raising globally, you need to replace the default exception handler. The attached initializer makes I18n just raise any exception (in a development or test environment).


Convention Decision: to i18n or not to i18n in spec assertions, Using translate! will cause i18n to raise an exception if the translation you're looking haven't already (it's default for newer versions of Rails), set i18n to raise exceptions in Put something like this in config/environments/test.rb # Raises error for missing translations config.action_view.​raise_on_missing_translations = true. flufl.i18n comes with a couple of fairly simple strategies, but you can of course write your own. A convenient built-in strategy looks up catalogs from within the package directory using GNU gettext ‘s convention, where the base directory for the catalogs is rooted in a subpackage.