From 3a056ad6fce28c2b8a93babde2d5a69329362b0c Mon Sep 17 00:00:00 2001 From: Xenor Chang Date: Mon, 8 May 2023 13:52:55 +0800 Subject: [PATCH 1/5] Fix I18n.t when locale contains separator --- lib/i18n.rb | 3 +-- test/i18n_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index 0075a360..ce2108ba 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -331,9 +331,8 @@ def with_locale(tmp_locale = nil) # keys are Symbols. def normalize_keys(locale, key, scope, separator = nil) separator ||= I18n.default_separator - locale = locale.to_sym if locale - result = [locale] + result = locale ? normalize_key(locale, separator) : [locale] result.concat(normalize_key(scope, separator)) if scope result.concat(normalize_key(key, separator)) end diff --git a/test/i18n_test.rb b/test/i18n_test.rb index eb6a200b..058d2c03 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -140,6 +140,10 @@ def setup assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|') end + test "normalize_keys normalizes given locale with separator" do + assert_equal [:en, :foo, :bar, :baz], I18n.normalize_keys(:"en.foo", :baz, :bar) + end + test "can set the exception_handler" do begin previous_exception_handler = I18n.exception_handler From ec044d8bbc1ff33dfacb24bbd43f3fc00ab626c8 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 31 May 2023 12:23:27 +1000 Subject: [PATCH 2/5] Correct translation missing checks --- lib/i18n/tests/localization/date.rb | 2 +- lib/i18n/tests/localization/date_time.rb | 2 +- lib/i18n/tests/localization/time.rb | 2 +- lib/i18n/tests/lookup.rb | 2 +- test/backend/exceptions_test.rb | 6 +++--- test/backend/fallbacks_test.rb | 4 ++-- test/backend/key_value_test.rb | 12 ++++++------ test/i18n/exceptions_test.rb | 4 ++-- test/i18n_test.rb | 8 ++++---- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/i18n/tests/localization/date.rb b/lib/i18n/tests/localization/date.rb index c06b7ece..c21fbbf3 100644 --- a/lib/i18n/tests/localization/date.rb +++ b/lib/i18n/tests/localization/date.rb @@ -64,7 +64,7 @@ def setup end test "localize Date: given missing translations it returns the correct error message" do - assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@date, :format => '%b', :locale => :fr) + assert_equal 'Translation missing: fr.date.abbr_month_names', I18n.l(@date, :format => '%b', :locale => :fr) end test "localize Date: given an unknown format it does not fail" do diff --git a/lib/i18n/tests/localization/date_time.rb b/lib/i18n/tests/localization/date_time.rb index b09b8880..b5d3527d 100644 --- a/lib/i18n/tests/localization/date_time.rb +++ b/lib/i18n/tests/localization/date_time.rb @@ -60,7 +60,7 @@ def setup end test "localize DateTime: given missing translations it returns the correct error message" do - assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@datetime, :format => '%b', :locale => :fr) + assert_equal 'Translation missing: fr.date.abbr_month_names', I18n.l(@datetime, :format => '%b', :locale => :fr) end test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator" do diff --git a/lib/i18n/tests/localization/time.rb b/lib/i18n/tests/localization/time.rb index 7afe176f..456a7602 100644 --- a/lib/i18n/tests/localization/time.rb +++ b/lib/i18n/tests/localization/time.rb @@ -61,7 +61,7 @@ def setup end test "localize Time: given missing translations it returns the correct error message" do - assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@time, :format => '%b', :locale => :fr) + assert_equal 'Translation missing: fr.date.abbr_month_names', I18n.l(@time, :format => '%b', :locale => :fr) end test "localize Time: given a meridian indicator format it returns the correct meridian indicator" do diff --git a/lib/i18n/tests/lookup.rb b/lib/i18n/tests/lookup.rb index 3bd46b8b..bbd775f0 100644 --- a/lib/i18n/tests/lookup.rb +++ b/lib/i18n/tests/lookup.rb @@ -30,7 +30,7 @@ def setup end test "lookup: given a missing key, no default and no raise option it returns an error message" do - assert_equal "translation missing: en.missing", I18n.t(:missing) + assert_equal "Translation missing: en.missing", I18n.t(:missing) end test "lookup: given a missing key, no default and the raise option it raises MissingTranslationData" do diff --git a/test/backend/exceptions_test.rb b/test/backend/exceptions_test.rb index cc221669..e19c8121 100644 --- a/test/backend/exceptions_test.rb +++ b/test/backend/exceptions_test.rb @@ -10,7 +10,7 @@ def setup exception = catch(:exception) do I18n.t(:'baz.missing', :scope => :'foo.bar', :throw => true) end - assert_equal "translation missing: en.foo.bar.baz.missing", exception.message + assert_equal "Translation missing: en.foo.bar.baz.missing", exception.message end test "exceptions: MissingTranslationData message from #translate includes the given scope and full key" do @@ -18,7 +18,7 @@ def setup I18n.t(:'baz.missing', :scope => :'foo.bar', :raise => true) rescue I18n::MissingTranslationData => exception end - assert_equal "translation missing: en.foo.bar.baz.missing", exception.message + assert_equal "Translation missing: en.foo.bar.baz.missing", exception.message end test "exceptions: MissingTranslationData message from #localize includes the given scope and full key" do @@ -26,7 +26,7 @@ def setup I18n.l(Time.now, :format => :foo) rescue I18n::MissingTranslationData => exception end - assert_equal "translation missing: en.time.formats.foo", exception.message + assert_equal "Translation missing: en.time.formats.foo", exception.message end test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do diff --git a/test/backend/fallbacks_test.rb b/test/backend/fallbacks_test.rb index 742db2e6..369e731d 100644 --- a/test/backend/fallbacks_test.rb +++ b/test/backend/fallbacks_test.rb @@ -60,8 +60,8 @@ def setup assert_nil I18n.t(:missing_bar, :locale => :'de-DE', :default => nil) end - test "returns the translation missing message if the default is also missing" do - assert_equal 'translation missing: de-DE.missing_bar', I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz]) + test "returns the Translation missing: message if the default is also missing" do + assert_equal 'Translation missing: de-DE.missing_bar', I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz]) end test "returns the :'de-DE' default :baz translation for a missing :'de-DE' when defaults contains Symbol" do diff --git a/test/backend/key_value_test.rb b/test/backend/key_value_test.rb index aefa9ce6..34bce7b0 100644 --- a/test/backend/key_value_test.rb +++ b/test/backend/key_value_test.rb @@ -21,7 +21,7 @@ def assert_flattens(expected, nested, escape=true, subtree=true) assert_flattens({:"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}}, true, false) assert_flattens({:"a.b" => "c"}, {:"a.b" => "c"}, false) end - + test "store_translations supports numeric keys" do setup_backend! store_translations(:en, 1 => 'foo') @@ -60,8 +60,8 @@ def assert_flattens(expected, nested, escape=true, subtree=true) I18n.backend.send(:translations) expected = { :en => {:foo => { :bar => 'bar', :baz => 'baz' }} } assert_equal expected, translations - end - + end + test "subtrees enabled: given incomplete pluralization data it raises I18n::InvalidPluralizationData" do setup_backend! store_translations(:en, :bar => { :one => "One" }) @@ -71,7 +71,7 @@ def assert_flattens(expected, nested, escape=true, subtree=true) test "subtrees disabled: given incomplete pluralization data it returns an error message" do setup_backend!(false) store_translations(:en, :bar => { :one => "One" }) - assert_equal "translation missing: en.bar", I18n.t(:bar, :count => 2) + assert_equal "Translation missing: en.bar", I18n.t(:bar, :count => 2) end test "translate handles subtrees for pluralization" do @@ -79,7 +79,7 @@ def assert_flattens(expected, nested, escape=true, subtree=true) store_translations(:en, :bar => { :one => "One" }) assert_equal("One", I18n.t("bar", :count => 1)) end - + test "subtrees enabled: returns localized string given missing pluralization data" do setup_backend!(true) assert_equal 'bar', I18n.t("foo.bar", count: 1) @@ -89,7 +89,7 @@ def assert_flattens(expected, nested, escape=true, subtree=true) setup_backend!(false) assert_equal 'bar', I18n.t("foo.bar", count: 1) end - + test "subtrees enabled: Returns fallback default given missing pluralization data" do setup_backend!(true) I18n.backend.extend I18n::Backend::Fallbacks diff --git a/test/i18n/exceptions_test.rb b/test/i18n/exceptions_test.rb index 3920bf91..4696d517 100644 --- a/test/i18n/exceptions_test.rb +++ b/test/i18n/exceptions_test.rb @@ -28,13 +28,13 @@ def test_invalid_locale_stores_locale test "MissingTranslationData message contains the locale and scoped key" do force_missing_translation_data do |exception| - assert_equal 'translation missing: de.bar.foo', exception.message + assert_equal 'Translation missing: de.bar.foo', exception.message end end test "MissingTranslationData message contains all potential options" do force_missing_translation_data(default: [:option_a, :option_b]) do |exception| - assert_equal "translation missing. Options considered were:\n- de.bar.option_a, \n- de.bar.option_a", exception.message + assert_equal "Translation missing:. Options considered were:\n- de.bar.option_a, \n- de.bar.option_a", exception.message end end diff --git a/test/i18n_test.rb b/test/i18n_test.rb index eb6a200b..925a4fce 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -205,16 +205,16 @@ def setup end # def test_translate_given_no_args_raises_missing_translation_data - # assert_equal "translation missing: en, no key", I18n.t + # assert_equal "Translation missing: en, no key", I18n.t # end test "translate given a bogus key returns an error message" do - assert_equal "translation missing: en.bogus", I18n.t(:bogus) + assert_equal "Translation missing: en.bogus", I18n.t(:bogus) end test "translate given multiple bogus keys returns an array of error messages" do assert_equal( - ["translation missing: en.bogus", "translation missing: en.also_bogus"], + ["Translation missing: en.bogus", "Translation missing: en.also_bogus"], I18n.t([:bogus, :also_bogus]), ) end @@ -262,7 +262,7 @@ def setup I18n.t('foo') end - assert_equal 'translation missing: en.foo', I18n.t('foo', locale: :en) + assert_equal 'Translation missing: en.foo', I18n.t('foo', locale: :en) end end From 76bac483976aefcfae88bd00e0bf66d4318e29e8 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 31 May 2023 12:35:02 +1000 Subject: [PATCH 3/5] Revert "make sure I18n.fallbacks updates itself" This reverts commit 5ccdd88747e07ab77c7b4d8564f082a934dfd3a9. --- lib/i18n/locale/fallbacks.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/i18n/locale/fallbacks.rb b/lib/i18n/locale/fallbacks.rb index a8f8b3e0..e30acc43 100644 --- a/lib/i18n/locale/fallbacks.rb +++ b/lib/i18n/locale/fallbacks.rb @@ -74,7 +74,6 @@ def map(*args, &block) @map[from] << _to.to_sym end end - replace @map else @map.map(*args, &block) end From 62818cf5cc752ba6744ad66af1d09de36e3c7c36 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 31 May 2023 12:40:03 +1000 Subject: [PATCH 4/5] Correct translation missing assertions --- test/backend/fallbacks_test.rb | 8 +++++++- test/i18n/exceptions_test.rb | 8 +------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/backend/fallbacks_test.rb b/test/backend/fallbacks_test.rb index 369e731d..6d61408f 100644 --- a/test/backend/fallbacks_test.rb +++ b/test/backend/fallbacks_test.rb @@ -61,7 +61,13 @@ def setup end test "returns the Translation missing: message if the default is also missing" do - assert_equal 'Translation missing: de-DE.missing_bar', I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz]) + translation_missing_message = <<~MSG + Translation missing. Options considered were: + - de-DE.missing_bar + - de-DE.missing_baz + MSG + + assert_equal translation_missing_message.chomp, I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz]) end test "returns the :'de-DE' default :baz translation for a missing :'de-DE' when defaults contains Symbol" do diff --git a/test/i18n/exceptions_test.rb b/test/i18n/exceptions_test.rb index 4696d517..771308d0 100644 --- a/test/i18n/exceptions_test.rb +++ b/test/i18n/exceptions_test.rb @@ -28,13 +28,7 @@ def test_invalid_locale_stores_locale test "MissingTranslationData message contains the locale and scoped key" do force_missing_translation_data do |exception| - assert_equal 'Translation missing: de.bar.foo', exception.message - end - end - - test "MissingTranslationData message contains all potential options" do - force_missing_translation_data(default: [:option_a, :option_b]) do |exception| - assert_equal "Translation missing:. Options considered were:\n- de.bar.option_a, \n- de.bar.option_a", exception.message + assert_equal 'translation missing: de.bar.foo', exception.message end end From 4aec070f00a73ed26555e76741fb94656ebda758 Mon Sep 17 00:00:00 2001 From: Xenor Chang Date: Wed, 31 May 2023 11:05:42 +0800 Subject: [PATCH 5/5] Revert normalized_keys before #651 --- lib/i18n.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index ce2108ba..d3369704 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -332,9 +332,11 @@ def with_locale(tmp_locale = nil) def normalize_keys(locale, key, scope, separator = nil) separator ||= I18n.default_separator - result = locale ? normalize_key(locale, separator) : [locale] - result.concat(normalize_key(scope, separator)) if scope - result.concat(normalize_key(key, separator)) + [ + *normalize_key(locale, separator), + *normalize_key(scope, separator), + *normalize_key(key, separator) + ] end # Returns true when the passed locale, which can be either a String or a