Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 276
Phương Nguyễn Avatar asked Nov 09 '11 15:11

Phương Nguyễn


3 Answers

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.

like image 61
GMA Avatar answered Nov 02 '22 07:11

GMA


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.

like image 41
eloyesp Avatar answered Nov 02 '22 05:11

eloyesp


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 }
like image 14
Wolfgang Avatar answered Nov 02 '22 06:11

Wolfgang