Rails:如何提高I18n的翻译在测试环境中是缺失的。

时间:2021-07-14 07:41:30

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?

我希望Rails在测试环境中丢失一个I18n转换(而不是呈现文本“translation missing”)时引发一个异常。有没有一种简单的方法来实现这一点?

4 个解决方案

#1


16  

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


16  

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:

从Rails 4.1.0开始,现在有一个比4年前回答这个问题更好的解决方案:在配置文件中添加以下内容:

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.

我喜欢只在测试环境中设置它,但是您也可以在开发中设置它。我强烈建议在生产中不要将其设置为true。

#3


9  

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

我已经创建了这个初始化器来引发一个异常——args被传递,所以您将知道哪个i18n键丢失了!

# 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

#4


9  

Rails 4.1+

Rails 4.1 +

To raise i18n translation missing exceptions you need two things:

要提出i18n翻译缺失的异常,需要以下两点:

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

1)初始化配置/初始化/ 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):

2)配置/环境/测试中的配置设置。rb(以及需要的其他环境):

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.

注意:除了异常处理程序之外,还需要配置设置,因为rails封装对I18n的调用。在它的视图和助手中进行翻译,防止异常触发。

#1


16  

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


16  

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:

从Rails 4.1.0开始,现在有一个比4年前回答这个问题更好的解决方案:在配置文件中添加以下内容:

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.

我喜欢只在测试环境中设置它,但是您也可以在开发中设置它。我强烈建议在生产中不要将其设置为true。

#3


9  

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

我已经创建了这个初始化器来引发一个异常——args被传递,所以您将知道哪个i18n键丢失了!

# 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

#4


9  

Rails 4.1+

Rails 4.1 +

To raise i18n translation missing exceptions you need two things:

要提出i18n翻译缺失的异常,需要以下两点:

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

1)初始化配置/初始化/ 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):

2)配置/环境/测试中的配置设置。rb(以及需要的其他环境):

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.

注意:除了异常处理程序之外,还需要配置设置,因为rails封装对I18n的调用。在它的视图和助手中进行翻译,防止异常触发。