Skip to content

Commit 110922c

Browse files
committed
Handle format() for TextElements, StringLiterals, and NumberLiterals
1 parent a499ba7 commit 110922c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

fluent/resource.lua

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ node_types.Pattern = class({
105105
_base = FluentNode,
106106
_init = function (self, node)
107107
self:super(node)
108-
-- D(self.elements)
109108
self:dedent()
110109
end,
111110
dedent = function (self)
@@ -138,8 +137,8 @@ node_types.Pattern = class({
138137
tablex.foreachi(self.elements, strip, striplen)
139138
end,
140139
format = function (self, parameters)
141-
local value = #self.elements >= 2 and "TODO" or self.elements[1].value
142-
-- TODO: parse elements and actually format a value
140+
local function evaluate (node) return node:format(parameters) end
141+
local value = table.concat(tablex.map(evaluate, self.elements), " ")
143142
return value, parameters
144143
end
145144
})
@@ -156,6 +155,9 @@ node_types.TextElement = class({
156155
self.value = (self.value or "") .. "\n" .. (node.value or "")
157156
return self
158157
end
158+
end,
159+
format = function (self)
160+
return self.value
159161
end
160162
})
161163

@@ -168,6 +170,9 @@ node_types.Placeable = class({
168170
if node.expression then
169171
self.expression = node_to_type(node.expression[1])
170172
end
173+
end,
174+
format = function (self)
175+
return self.expression.value
171176
end
172177
})
173178

@@ -183,20 +188,29 @@ node_types.StringLiteral = class({
183188
_base = FluentNode,
184189
_init = function (self, node)
185190
self:super(node)
191+
end,
192+
format = function (self)
193+
return self.value
186194
end
187195
})
188196

189197
node_types.NumberLiteral = class({
190198
_base = FluentNode,
191199
_init = function (self, node)
192200
self:super(node)
201+
end,
202+
format = function (self)
203+
return self.value
193204
end
194205
})
195206

196207
node_types.VariableReference = class({
197208
_base = FluentNode,
198209
_init = function (self, node)
199210
self:super(node)
211+
end,
212+
format = function (self)
213+
return self.value
200214
end
201215
})
202216

spec/fluent_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ describe('fluent.bundle', function ()
2828
assert.same("baz", en:format("bar"))
2929
end)
3030

31+
it('should parse and format literals', function ()
32+
local en = FluentBundle("en-US")
33+
en:add_messages('foo = bar {"baz"} quz {-3.14}')
34+
assert.same("bar baz quz -3.14", en:format("foo"))
35+
end)
36+
3137
it('should keep locale instances separate', function ()
3238
local en = FluentBundle("en-US")
3339
local tr = FluentBundle("tr-TR")

0 commit comments

Comments
 (0)