diff --git a/lib/i18n/backend/chain.rb b/lib/i18n/backend/chain.rb index cb6ad43b..01d2ec98 100644 --- a/lib/i18n/backend/chain.rb +++ b/lib/i18n/backend/chain.rb @@ -17,7 +17,7 @@ module Backend class Chain module Implementation include Base - + attr_accessor :backends def initialize(*backends) @@ -45,7 +45,7 @@ def translate(locale, key, default_options = {}) options = default_options if backend == backends.last translation = backend.translate(locale, key, options) if namespace_lookup?(translation, options) - namespace = translation.merge(namespace || {}) + namespace = _deep_merge(translation, namespace || {}) elsif !translation.nil? return translation end @@ -75,6 +75,20 @@ def localize(locale, object, format = :default, options = {}) def namespace_lookup?(result, options) result.is_a?(Hash) && !options.has_key?(:count) end + + private + # This is approximately what gets used in ActiveSupport. + # However since we are not guaranteed to run in an ActiveSupport context + # it is wise to have our own copy. We underscore it + # to not pollute the namespace of the including class. + def _deep_merge(hash, other_hash) + copy = hash.dup + other_hash.each_pair do |k,v| + value_from_other = hash[k] + copy[k] = value_from_other.is_a?(Hash) && v.is_a?(Hash) ? _deep_merge(value_from_other, v) : v + end + copy + end end include Implementation diff --git a/test/backend/chain_test.rb b/test/backend/chain_test.rb index a0643374..0b402af2 100644 --- a/test/backend/chain_test.rb +++ b/test/backend/chain_test.rb @@ -4,10 +4,20 @@ class I18nBackendChainTest < I18n::TestCase def setup super @first = backend(:en => { - :foo => 'Foo', :formats => { :short => 'short' }, :plural_1 => { :one => '%{count}' }, :dates => {:a => "A"} + :foo => 'Foo', :formats => { + :short => 'short', + :subformats => {:short => 'short'}, + }, + :plural_1 => { :one => '%{count}' }, + :dates => {:a => "A"} }) @second = backend(:en => { - :bar => 'Bar', :formats => { :long => 'long' }, :plural_2 => { :one => 'one' }, :dates => {:a => "B", :b => "B"} + :bar => 'Bar', :formats => { + :long => 'long', + :subformats => {:long => 'long'}, + }, + :plural_2 => { :one => 'one' }, + :dates => {:a => "B", :b => "B"} }) @chain = I18n.backend = I18n::Backend::Chain.new(@first, @second) end @@ -39,11 +49,11 @@ def setup assert_equal({}, I18n.t(:'i18n.transliterate.rule', :locale => 'en', :default => {})) end - test "namespace lookup collects results from all backends" do - assert_equal({ :short => 'short', :long => 'long' }, I18n.t(:formats)) + test "namespace lookup collects results from all backends and merges deep hashes" do + assert_equal({:long=>"long", :subformats=>{:long=>"long", :short=>"short"}, :short=>"short"}, I18n.t(:formats)) end - test "namespace lookup collects results from all backends and does not overwrite" do + test "namespace lookup collects results from all backends and lets leftmost backend take priority" do assert_equal({ :a => "A", :b => "B" }, I18n.t(:dates)) end @@ -59,7 +69,11 @@ def setup test "bulk lookup collects results from all backends" do assert_equal ['Foo', 'Bar'], I18n.t([:foo, :bar]) assert_equal ['Foo', 'Bar', 'Bah'], I18n.t([:foo, :bar, :bah], :default => 'Bah') - assert_equal [{ :short => 'short', :long => 'long' }, { :one => 'one' }, 'Bah'], I18n.t([:formats, :plural_2, :bah], :default => 'Bah') + assert_equal [{ + :long=>"long", + :subformats=>{:long=>"long", :short=>"short"}, + :short=>"short"}, {:one=>"one"}, + "Bah"], I18n.t([:formats, :plural_2, :bah], :default => 'Bah') end test "store_translations options are not dropped while transfering to backend" do