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

Chain fallback backends #499

Merged
merged 2 commits into from
Jan 8, 2020
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
3 changes: 1 addition & 2 deletions lib/i18n/backend/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def translations
init_translations unless initialized?
translations
end

memo.deep_merge!(partial_translations)
memo.deep_merge!(partial_translations) { |_, a, b| b || a }
end
end

Expand Down
16 changes: 11 additions & 5 deletions lib/i18n/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ def deep_symbolize_keys
end
end

# deep_merge_hash! by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
def deep_merge!(data)
merger = lambda do |_key, v1, v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
# deep_merge! from activesupport 5
# Copyright (c) 2005-2019 David Heinemeier Hansson
def deep_merge!(other_hash, &block)
merge!(other_hash) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
this_val.deep_merge(other_val, &block)
elsif block_given?
block.call(key, this_val, other_val)
else
other_val
end
end
merge!(data, &merger)
end

def symbolize_key(key)
Expand Down
41 changes: 26 additions & 15 deletions test/backend/chain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ def setup
:subformats => {:short => 'short'},
},
:plural_1 => { :one => '%{count}' },
:dates => {:a => "A"}
:dates => {:a => "A"},
:fallback_bar => nil,
})
@second = backend(:en => {
:bar => 'Bar', :formats => {
:long => 'long',
:subformats => {:long => 'long'},
},
:plural_2 => { :one => 'one' },
:dates => {:a => "B", :b => "B"}
:dates => {:a => "B", :b => "B"},
:fallback_bar => 'Bar',
})
@chain = I18n.backend = I18n::Backend::Chain.new(@first, @second)
end
Expand Down Expand Up @@ -107,20 +109,29 @@ def setup
assert @second.initialized?
end

test "falls back to other backends for nil values" do
assert_nil @first.send(:translations)[:en][:fallback_bar]
assert_equal 'Bar', @second.send(:translations)[:en][:fallback_bar]
assert_equal 'Bar', I18n.t(:fallback_bar)
end

test 'should be able to get all translations of all backends merged together' do
assert_equal I18n.backend.send(:translations),
en: {
foo: 'Foo',
bar: 'Bar',
formats: {
short: 'short',
long: 'long',
subformats: { short: 'short', long: 'long' }
},
plural_1: { one: "%{count}" },
plural_2: { one: 'one' },
dates: { a: 'A', b: 'B' }
}
expected = {
en: {
foo: 'Foo',
bar: 'Bar',
formats: {
short: 'short',
long: 'long',
subformats: { short: 'short', long: 'long' }
},
plural_1: { one: "%{count}" },
plural_2: { one: 'one' },
dates: { a: 'A', b: 'B' },
fallback_bar: 'Bar'
}
}
assert_equal expected, I18n.backend.send(:translations)
end

protected
Expand Down