How do you save I18n :: MissingTranslationData?

I want to save I18n::MissingTranslationDatalike this:

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

I tried above, but it seems that he did not fall into the rescue unit. I just see on my console (due to puts): translation missing: some.key.that.does.not.exist. I never see Kaboom!.

How do I make this work?

+3
source share
4 answers

IMO, this is rather strange, but in the current version i18n (0.5.0)you have to pass the exception you want to save:

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

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

+6
source

Same as above, but better .

v = "doesnt_exist"
begin
  puts I18n.t "langs.#{v}", raise: true
rescue
  puts "Nooo #{v} has no Translation!"
end

or

puts I18n.t("langs.#{v}", default: "No Translation!")

or

a = I18n.t "langs.#{v}", raise: true rescue false
unless a
  puts "Update your YAML!"
end
+2
source

I18n , , MissingTranslation. I18n ArgumentError . , , .

i18n 6.2 RailsGuides I18n

0

, :raise => true

assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }

..., I18n::MissingTranslationData.

. https://github.com/svenfuchs/i18n/blob/master/lib/i18n/tests/lookup.rb

0

All Articles