【问题标题】:Ruby i18n.translate having :raise => true as default behaviourRuby i18n.translate 具有 :raise => true 作为默认行为
【发布时间】:2014-12-29 23:27:30
【问题描述】:

当我打电话时:

t 'to_translate'

我的模板中的方法,我希望它在不实际传递第二个参数的情况下引发错误,如下所示:

t 'to_translate', :raise => true

每次翻译时,我都会节省大量的打字时间。或者有解决方法吗?

感谢您的回答。

【问题讨论】:

    标签: ruby-on-rails ruby rails-i18n i18n-gem


    【解决方案1】:

    你可以在I18n documentation第6.2段找到答案

    "[..] 默认异常处理不允许在自动化测试期间轻松捕获丢失的翻译。为此,可以指定不同的异常处理程序。指定的异常处理程序必须是 I18n 模块或类上的方法使用#call方法:

    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
    

    这只会重新引发 MissingTranslationData 异常,将所有其他输入传递给默认异常处理程序。"

    了解更多 http://guides.rubyonrails.org/i18n.html#using-different-exception-handlers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多