From 2a5dd853a2b89a9bfd0a8876336e025dbcd9c308 Mon Sep 17 00:00:00 2001 From: Bartuz Date: Tue, 26 Aug 2014 00:30:12 +0200 Subject: [PATCH 1/6] Interpolate now works for array --- lib/i18n/backend/base.rb | 2 ++ lib/i18n/tests/interpolation.rb | 6 ++++++ lib/i18n/tests/lookup.rb | 6 +++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index c8bb070f..9ed59c1f 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -154,6 +154,8 @@ def pluralize(locale, entry, count) def interpolate(locale, string, values = {}) if string.is_a?(::String) && !values.empty? I18n.interpolate(string, values) + elsif string.is_a?(::Array) && !values.empty? + string.map { |el| interpolate(locale, el, values) } else string end diff --git a/lib/i18n/tests/interpolation.rb b/lib/i18n/tests/interpolation.rb index c457b3d7..f889a9d2 100644 --- a/lib/i18n/tests/interpolation.rb +++ b/lib/i18n/tests/interpolation.rb @@ -54,6 +54,12 @@ module Interpolation assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda') end + test "interpolation: given an array interpolates each element" do + I18n.backend.store_translations(:en, :array_interpolate => ['Hi', '%{name}']) + assert_equal ['Hi', 'Bartuz'], interpolate(:array_interpolate, :name => 'Bartuz') + end + + test "interpolation: given the translation is in utf-8 it still works" do assert_equal 'Häi David!', interpolate(:default => 'Häi %{name}!', :name => 'David') end diff --git a/lib/i18n/tests/lookup.rb b/lib/i18n/tests/lookup.rb index 3b4c8434..a9bc1740 100644 --- a/lib/i18n/tests/lookup.rb +++ b/lib/i18n/tests/lookup.rb @@ -6,7 +6,7 @@ module Lookup def setup super I18n.backend.store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' }, :falsy => false, :truthy => true, - :string => "a", :array => %w(a b c), :hash => { "a" => "b" }) + :string => "a", :array => %w(a b c), :interpolate_array => %w(foo %{bar}), :hash => { "a" => "b" }) end test "lookup: it returns a string" do @@ -21,6 +21,10 @@ def setup assert_equal(%w(a b c), I18n.t(:array)) end + test "lookup: it returns the interpolate_array" do + assert_equal ['foo', '%{bar}'], I18n.t(:interpolate_array) + end + test "lookup: it returns a native true" do assert I18n.t(:truthy) === true end From 321bc1b4d2702e1b7c343c8d000181bdc6b573c2 Mon Sep 17 00:00:00 2001 From: Filip Bartuzi Date: Tue, 26 Aug 2014 01:17:34 +0200 Subject: [PATCH 2/6] Update interpolation.rb removes unnecessary line --- lib/i18n/tests/interpolation.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/i18n/tests/interpolation.rb b/lib/i18n/tests/interpolation.rb index f889a9d2..d5c468f2 100644 --- a/lib/i18n/tests/interpolation.rb +++ b/lib/i18n/tests/interpolation.rb @@ -59,7 +59,6 @@ module Interpolation assert_equal ['Hi', 'Bartuz'], interpolate(:array_interpolate, :name => 'Bartuz') end - test "interpolation: given the translation is in utf-8 it still works" do assert_equal 'Häi David!', interpolate(:default => 'Häi %{name}!', :name => 'David') end From ae859ddc799d7e6745b5838eceea34ce76cb556a Mon Sep 17 00:00:00 2001 From: bartuz Date: Sat, 12 Sep 2015 20:48:16 +0100 Subject: [PATCH 3/6] Refactor interpolation method Suggested by @clemens --- lib/i18n/backend/base.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index 9ed59c1f..c9163413 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -151,13 +151,15 @@ def pluralize(locale, entry, count) # # interpolate "file %{file} opened by %%{user}", :file => 'test.txt', :user => 'Mr. X' # # => "file test.txt opened by %{user}" - def interpolate(locale, string, values = {}) - if string.is_a?(::String) && !values.empty? - I18n.interpolate(string, values) - elsif string.is_a?(::Array) && !values.empty? - string.map { |el| interpolate(locale, el, values) } + + def interpolate(locale, subject, values = {}) + return subject if values.empty? + + case subject + when ::String then I18n.interpolate(subject, values) + when ::Array then subject.map { |element| interpolate(locale, element, values) } else - string + subject end end From 23069aa8e6b7716bb9347970fd5e6d5a375b6e85 Mon Sep 17 00:00:00 2001 From: bartuz Date: Sat, 12 Sep 2015 20:54:27 +0100 Subject: [PATCH 4/6] Add comments exaplining new array behaviour for #interpolate --- lib/i18n/backend/base.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index c9163413..02c46b0e 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -147,10 +147,17 @@ def pluralize(locale, entry, count) entry[key] end - # Interpolates values into a given string. + # Interpolates values into a given subject. # - # interpolate "file %{file} opened by %%{user}", :file => 'test.txt', :user => 'Mr. X' + # if the given subject is a string then: + # method interpolates "file %{file} opened by %%{user}", :file => 'test.txt', :user => 'Mr. X' # # => "file test.txt opened by %{user}" + # + # if the given subject is an array then: + # each element of the array is recursively interpolated (until it finds a string) + # method interpolates ["yes, %{user}", ["maybe no, %{user}, "no, %{user}"]], :user => "bartuz" + # # => "["yes, bartuz",["maybe no, bartuz", "no, bartuz"]]" + def interpolate(locale, subject, values = {}) return subject if values.empty? From 80e27e908a58a33218c8bc89c1c89cc0f20566f4 Mon Sep 17 00:00:00 2001 From: bartuz Date: Sat, 12 Sep 2015 21:01:02 +0100 Subject: [PATCH 5/6] Fix specs for array interpolation It covers full use case now. Thankfully to https://github.com/svenfuchs/i18n/pull/282#discussion_r37779978 --- lib/i18n/tests/interpolation.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/i18n/tests/interpolation.rb b/lib/i18n/tests/interpolation.rb index d5c468f2..27c99c3b 100644 --- a/lib/i18n/tests/interpolation.rb +++ b/lib/i18n/tests/interpolation.rb @@ -55,8 +55,8 @@ module Interpolation end test "interpolation: given an array interpolates each element" do - I18n.backend.store_translations(:en, :array_interpolate => ['Hi', '%{name}']) - assert_equal ['Hi', 'Bartuz'], interpolate(:array_interpolate, :name => 'Bartuz') + I18n.backend.store_translations(:en, :array_interpolate => ['Hi', 'Mr. %{name}', 'or sir %{name}']) + assert_equal ['Hi', 'Mr. Bartuz', 'or sir Bartuz'], interpolate(:array_interpolate, :name => 'Bartuz') end test "interpolation: given the translation is in utf-8 it still works" do From 7ffc4967570436d8560cdfaaac2acb12c0bfbdc8 Mon Sep 17 00:00:00 2001 From: bartuz Date: Sat, 12 Sep 2015 21:12:16 +0100 Subject: [PATCH 6/6] Remove redundant test for lookup This reverts PART of commit ee27f3d85ab275757b144aa216c1a282ef9bb345. thanks @clemens https://github.com/svenfuchs/i18n/pull/282/files#r37780129 --- lib/i18n/tests/lookup.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/i18n/tests/lookup.rb b/lib/i18n/tests/lookup.rb index a9bc1740..3b4c8434 100644 --- a/lib/i18n/tests/lookup.rb +++ b/lib/i18n/tests/lookup.rb @@ -6,7 +6,7 @@ module Lookup def setup super I18n.backend.store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' }, :falsy => false, :truthy => true, - :string => "a", :array => %w(a b c), :interpolate_array => %w(foo %{bar}), :hash => { "a" => "b" }) + :string => "a", :array => %w(a b c), :hash => { "a" => "b" }) end test "lookup: it returns a string" do @@ -21,10 +21,6 @@ def setup assert_equal(%w(a b c), I18n.t(:array)) end - test "lookup: it returns the interpolate_array" do - assert_equal ['foo', '%{bar}'], I18n.t(:interpolate_array) - end - test "lookup: it returns a native true" do assert I18n.t(:truthy) === true end