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

我怕爱的太早我们不能终老 提交于 2020-01-12 06:31:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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 }



回答6:


If you're using rails between 4.0.0 to 4.1.0 you should monkey patch this way:

module ActionView::Helpers::TranslationHelper
  def t_with_raise(*args)
    value = t_without_raise(*args)

    if value.to_s.match(/title="translation missing: (.+)"/)
      raise "Translation missing: #{$1}"
    else
      value
    end
  end
  alias_method :translate_with_raise, :t_with_raise

  alias_method_chain :t, :raise
  alias_method_chain :translate, :raise
end


来源:https://stackoverflow.com/questions/8066901/rails-how-to-raise-i18n-translation-is-missing-exceptions-in-the-testing-enviro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!