当我打电话时:
t 'to_translate'
我的模板中的方法,我希望它在不实际传递第二个参数的情况下引发错误,如下所示:
t 'to_translate', :raise => true
这将为我在每次翻译时节省大量打字时间。或者有解决方法吗?
感谢您的回答。
最佳答案
您可以在 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
关于ruby-on-rails - Ruby i18n.translate 具有 :raise => true as default behaviour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27697510/