Skip to content

Commit

Permalink
Interpolate now works for array
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartuz authored and stereobooster committed Nov 9, 2017
1 parent 4e9b237 commit 2a5dd85
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/i18n/backend/base.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/i18n/tests/interpolation.rb
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tests/lookup.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2a5dd85

Please sign in to comment.