您如何救援I18n :: MissingTranslationData? [英] How do you rescue I18n::MissingTranslationData?

查看:74
本文介绍了您如何救援I18n :: MissingTranslationData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够像这样挽救I18n::MissingTranslationData:

I want to be able to rescue I18n::MissingTranslationData like so:

begin
  value = I18n.t('some.key.that.does.not.exist')
  puts value
  return value if value
rescue I18n::MissingTranslationData
  puts "Kaboom!"
end

我尝试了上面的方法,但是它似乎并没有进入救援的范围.我只是在控制台上看到(由于puts):translation missing: some.key.that.does.not.exist.我从没见过Kaboom!.

I tried the above, but it doesn't seem to go into the rescue block. I just see, on my console (because of puts): translation missing: some.key.that.does.not.exist. I never see Kaboom!.

我如何使它工作?

推荐答案

IMO,这很奇怪,但是在i18n (0.5.0)的当前版本中,您应该传递一个要救援的异常:

IMO, it's pretty strange but in the current version of i18n (0.5.0) you should pass an exception that you want to rescue:

require 'i18n'
begin
  value = I18n.translate('some.key.that.does.not.exist', :raise => I18n::MissingTranslationData)
  puts value
  return value if value
rescue I18n::MissingTranslationData
  puts "Kaboom!"
end

,它将在将来的0.6版本中修复(您可以对其进行测试- https://github.com/svenfuchs/i18n )

and it will be fixed in the future 0.6 release (you can test it - https://github.com/svenfuchs/i18n)

这篇关于您如何救援I18n :: MissingTranslationData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆