Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix I18n.t when locale contains separator #656

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,12 @@ 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.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
Expand Down
1 change: 0 additions & 1 deletion lib/i18n/locale/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def map(*args, &block)
@map[from] << _to.to_sym
end
end
replace @map
else
@map.map(*args, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tests/localization/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tests/localization/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tests/localization/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tests/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/backend/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ 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
begin
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
begin
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
Expand Down
10 changes: 8 additions & 2 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ 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
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
Expand Down
12 changes: 6 additions & 6 deletions test/backend/key_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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" })
Expand All @@ -71,15 +71,15 @@ 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
setup_backend!(false)
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)
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/i18n/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ def test_invalid_locale_stores_locale
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
end
end

test "InvalidPluralizationData stores entry, count and key" do
force_invalid_pluralization_data do |exception|
assert_equal({:other => "bar"}, exception.entry)
Expand Down
12 changes: 8 additions & 4 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -205,16 +209,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
Expand Down Expand Up @@ -262,7 +266,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

Expand Down