Skip to content

Commit

Permalink
Fix bug which overwrote source strings in arrays when interpolating
Browse files Browse the repository at this point in the history
  • Loading branch information
personalnadir committed Nov 5, 2019
1 parent 652c265 commit a0ada42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion i18n/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ end

local function treatNode(node, data)
if isArray(node) then
for k,v in ipairs(node) do
local iter = {ipairs(node)}
node = {}
for k,v in unpack(iter) do
node[k] = treatNode(v, data)
end
elseif type(node) == 'string' then
Expand Down
18 changes: 17 additions & 1 deletion spec/i18n_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ describe('i18n', function()
},i18n('suede',{count = "one", show = "two", ready = "three"}))
end)

it("Interpolation produces different results when called with different values", function()
i18n.set('en.suede', {"%{count} for the count", "%{show} for the show", "%{ready} to make ready", "go!"})
assert.same({
"one for the count",
"two for the show",
"three to make ready",
"go!"
},i18n('suede',{count = "one", show = "two", ready = "three"}))

assert.same({
"a for the count",
"b for the show",
"c to make ready",
"go!"
},i18n('suede',{count = "a", show = "b", ready = "c"}))
end)

it("Variables in array elements can be pluralized", function()
i18n.set('en.safe', {"Welcome to Apature!", {
one = "%{count} unfortunate retirement today!",
Expand Down Expand Up @@ -217,7 +234,6 @@ describe('i18n', function()
"It's mostly safe!",
},i18n('safe',{count = 2}))
end)

end)

describe('set/getFallbackLocale', function()
Expand Down

0 comments on commit a0ada42

Please sign in to comment.