Skip to content

Commit

Permalink
Merge pull request #499 from vipera/chain-fallback-backends
Browse files Browse the repository at this point in the history
Chain fallback backends
  • Loading branch information
radar committed Jan 8, 2020
2 parents d139464 + bdae701 commit 719a36a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
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

0 comments on commit 719a36a

Please sign in to comment.