From 38a323e89bd5fcfc0b673d5069d850a22f61e013 Mon Sep 17 00:00:00 2001 From: Gordon Date: Fri, 15 Feb 2019 09:25:27 -0600 Subject: [PATCH 1/3] I18n::Backend::Chain#translations merge from all backends https://github.com/fnando/i18n-js uses the `#translations` method to dump all translations to a javascript file during asset compilation. The chain backend should deep_merge all these translations in order to dump the appropriate values. --- lib/i18n/backend/chain.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/i18n/backend/chain.rb b/lib/i18n/backend/chain.rb index b3640ab8..09986018 100644 --- a/lib/i18n/backend/chain.rb +++ b/lib/i18n/backend/chain.rb @@ -92,9 +92,13 @@ def init_translations end def translations - backends.first.instance_eval do - init_translations unless initialized? - translations + backends.reverse.inject({}) do |h, b| + to_merge = b.instance_eval do + init_translations unless initialized? + translations + end + + h.deep_merge(to_merge) end end From daa63b4cc531f61d9fb1da6f40bf89801d0ef6ac Mon Sep 17 00:00:00 2001 From: Gordon Date: Thu, 7 Mar 2019 08:29:52 -0600 Subject: [PATCH 2/3] Update spec for new behavior of chain backend --- test/backend/chain_test.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/backend/chain_test.rb b/test/backend/chain_test.rb index 18884e74..ce7e49fe 100644 --- a/test/backend/chain_test.rb +++ b/test/backend/chain_test.rb @@ -101,16 +101,19 @@ def setup assert !@second.initialized? end - test 'should be able to get all translations of the first backend' do + 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', - subformats: { short: 'short' } + long: 'long', + subformats: { short: 'short', long: 'long' } }, plural_1: { one: "%{count}" }, - dates: { a: 'A' } + plural_2: { one: 'one' }, + dates: { a: 'A', b: 'B' } } end From d2989389e3fdd9194cd1d3d3b78988f14cda23c9 Mon Sep 17 00:00:00 2001 From: Gordon Date: Thu, 7 Mar 2019 08:30:05 -0600 Subject: [PATCH 3/3] Use each_with_object and more descriptive names --- lib/i18n/backend/chain.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/i18n/backend/chain.rb b/lib/i18n/backend/chain.rb index 09986018..62baaea1 100644 --- a/lib/i18n/backend/chain.rb +++ b/lib/i18n/backend/chain.rb @@ -92,13 +92,13 @@ def init_translations end def translations - backends.reverse.inject({}) do |h, b| - to_merge = b.instance_eval do + backends.reverse.each_with_object({}) do |backend, memo| + partial_translations = backend.instance_eval do init_translations unless initialized? translations end - h.deep_merge(to_merge) + memo.deep_merge!(partial_translations) end end