From 5611700aed43f6bd6a5648298f60552b460af301 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Sun, 5 Jun 2022 23:47:42 -0400 Subject: [PATCH 1/2] Add test for existing bulk lookup failure behavior Currently, doing a bulk lookup for multiple keys returns only the "translation missing" string for the first key. This adds a test capturing that. --- test/i18n_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/i18n_test.rb b/test/i18n_test.rb index a0b81056..31f03b32 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -212,6 +212,13 @@ def setup assert_equal "translation missing: en.bogus", I18n.t(:bogus) end + test "translate given multiple bogus keys returns the first error message" do + assert_equal( + "translation missing: en.bogus", + I18n.t([:bogus, :also_bogus]), + ) + end + test "translate given an empty string as a key raises an I18n::ArgumentError" do assert_raises(I18n::ArgumentError) { I18n.t("") } end From 8a698d77c2365280e7ea6a653900a3eb33c592e6 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Sun, 5 Jun 2022 23:48:05 -0400 Subject: [PATCH 2/2] Consistently return array on bulk lookup In the event of we fail to lookup one or more keys during a bulk lookup, we should still return an array of messages. --- lib/i18n.rb | 26 ++++++++++++++++---------- test/i18n_test.rb | 4 ++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index e197e2b1..d3369704 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -214,18 +214,12 @@ def translate(key = nil, throw: false, raise: false, locale: nil, **options) # T backend = config.backend - result = catch(:exception) do - if key.is_a?(Array) - key.map { |k| backend.translate(locale, k, options) } - else - backend.translate(locale, key, options) + if key.is_a?(Array) + key.map do |k| + translate_key(k, throw, raise, locale, backend, options) end - end - - if result.is_a?(MissingTranslation) - handle_exception((throw && :throw || raise && :raise), result, locale, key, options) else - result + translate_key(key, throw, raise, locale, backend, options) end end alias :t :translate @@ -364,6 +358,18 @@ def available_locales_initialized? private + def translate_key(key, throw, raise, locale, backend, options) + result = catch(:exception) do + backend.translate(locale, key, options) + end + + if result.is_a?(MissingTranslation) + handle_exception((throw && :throw || raise && :raise), result, locale, key, options) + else + result + end + end + # Any exceptions thrown in translate will be sent to the @@exception_handler # which can be a Symbol, a Proc or any other Object unless they're forced to # be raised or thrown (MissingTranslation). diff --git a/test/i18n_test.rb b/test/i18n_test.rb index 31f03b32..7fa8fe1b 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -212,9 +212,9 @@ def setup assert_equal "translation missing: en.bogus", I18n.t(:bogus) end - test "translate given multiple bogus keys returns the first error message" do + test "translate given multiple bogus keys returns an array of error messages" do assert_equal( - "translation missing: en.bogus", + ["translation missing: en.bogus", "translation missing: en.also_bogus"], I18n.t([:bogus, :also_bogus]), ) end