Skip to content

Commit 1864b2f

Browse files
authored
Merge pull request #31 from alerque/property-access
2 parents 41446f6 + 226ce68 commit 1864b2f

File tree

5 files changed

+215
-101
lines changed

5 files changed

+215
-101
lines changed

fluent/_nodes.lua

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function FluentNode:_init (ast, resource)
3232
local node = leaf_to_node(leaf, resource)
3333
self:inject(node)
3434
end)
35+
return self
3536
end
3637

3738
function FluentNode:set_parent (resource)
@@ -99,6 +100,7 @@ function FTL.blank_block:_init (ast, resource)
99100
self:super(ast, resource)
100101
local _, count = string.gsub(ast[1], "\n", "")
101102
getmetatable(self).discardable = count == 0
103+
return self
102104
end
103105

104106
FTL.Entry = function (ast, resource)
@@ -114,7 +116,16 @@ FTL.Message._name = "Message"
114116
function FTL.Message:_init (ast, resource)
115117
self.attributes = setmetatable({}, { map = {} })
116118
self:super(ast, resource)
119+
-- Work around Penlight #307
117120
-- self:catch(self.get_attribute)
121+
self:_patch_init()
122+
return self
123+
end
124+
125+
function FTL.Message:_patch_init ()
126+
if not type(rawget(getmetatable(self), "__index")) ~= "function" then
127+
self:catch(function(_, attribute) return self:get_attribute(attribute) end)
128+
end
118129
end
119130

120131
function FTL.Message:set_attribute (attribute)
@@ -169,6 +180,7 @@ function FTL.Pattern:_init (ast, resource)
169180
self.elements = {}
170181
self:super(ast, resource)
171182
self:dedent()
183+
return self
172184
end
173185

174186
function FTL.Pattern:dedent ()
@@ -223,7 +235,7 @@ FTL.TextElement._name ="TextElement"
223235
function FTL.TextElement:_init (ast, resource)
224236
getmetatable(self).appendable = true
225237
ast.id = "TextElement"
226-
self:super(ast, resource)
238+
return self:super(ast, resource)
227239
end
228240

229241
function FTL.TextElement:__add (node)
@@ -244,7 +256,7 @@ function FTL.Placeable:_init (ast, resource)
244256
getmetatable(self).appendable = true
245257
ast.id = "Placeable"
246258
ast.expression = leaf_to_node(ast.expression, resource)
247-
self:super(ast, resource)
259+
return self:super(ast, resource)
248260
end
249261

250262
function FTL.Placeable:__mod (node)
@@ -315,7 +327,7 @@ FTL.TermReference._name = "TermReference"
315327

316328
function FTL.TermReference:_init (ast, resource)
317329
ast.id = "TermReference"
318-
self:super(ast, resource)
330+
return self:super(ast, resource)
319331
end
320332

321333
function FTL.TermReference:__mul (node)
@@ -350,7 +362,7 @@ function FTL.SelectExpression:_init (ast, resource)
350362
ast.id = "SelectExpression"
351363
self.selector = {}
352364
self.variants = {}
353-
self:super(ast, resource)
365+
return self:super(ast, resource)
354366
end
355367

356368
function FTL.SelectExpression:format (parameters)
@@ -385,7 +397,7 @@ FTL.variant_list._name = "variant_list"
385397

386398
function FTL.variant_list:_init (ast, resource)
387399
self.elements = {}
388-
self:super(ast, resource)
400+
return self:super(ast, resource)
389401
end
390402

391403
function FTL.variant_list:__mod (node)
@@ -401,7 +413,7 @@ FTL.Variant._name = "Variant"
401413
function FTL.Variant:_init (ast, resource)
402414
ast.id = "Variant"
403415
ast.default = ast.default or false
404-
self:super(ast, resource)
416+
return self:super(ast, resource)
405417
end
406418

407419
FTL.VariantKey = class(FluentNode)
@@ -425,7 +437,7 @@ FTL.CallArguments._name = "CallArguments"
425437
function FTL.CallArguments:_init (ast, resource)
426438
self.named = {}
427439
self.positional = {}
428-
self:super(ast, resource)
440+
return self:super(ast, resource)
429441
end
430442

431443
function FTL.CallArguments:__mul (node)
@@ -443,7 +455,7 @@ FTL.Comment._name = "Comment"
443455

444456
function FTL.Comment:_init (ast, resource)
445457
getmetatable(self).appendable = true
446-
self:super(ast, resource)
458+
return self:super(ast, resource)
447459
end
448460

449461
function FTL.Comment:__add (node)

fluent/init.lua

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,64 @@ local FluentSyntax = require("fluent.syntax")
88
local FluentResource = require("fluent.resource")
99

1010
local FluentBundle = class()
11-
FluentBundle.locales = {}
12-
FluentBundle.locale = "und"
1311

1412
function FluentBundle:_init (locale)
13+
self.locales = {}
1514
self:set_locale(locale)
15+
-- Work around Penlight #307
1616
-- self:catch(self.get_message)
17+
self:_patch_init()
18+
return self
19+
end
20+
21+
function FluentBundle:_patch_init ()
22+
if not type(rawget(getmetatable(self), "__index")) ~= "function" then
23+
self:catch(function(_, identifier) return self:get_message(identifier) end)
24+
end
1725
end
1826

1927
function FluentBundle:set_locale (locale)
2028
self.locale = CLDR.locales[locale] and locale or "und"
2129
if not self.locales[self.locale] then
2230
self.locales[self.locale] = FluentResource()
2331
end
32+
return self:get_locale()
2433
end
2534

26-
function FluentBundle:get_message (identifier)
35+
function FluentBundle:get_locale ()
36+
return self.locale
37+
end
38+
39+
function FluentBundle:get_resource (locale)
2740
local locales = self.locales
28-
local locale = self.locale
29-
local resource = locales[locale]
41+
local resource = locales[locale or self:get_locale()]
42+
resource._patch_init(resource)
43+
return resource
44+
end
45+
46+
function FluentBundle:get_message (identifier)
47+
local resource = self:get_resource()
3048
-- TODO iterate over fallback locales if not found in current one
31-
return resource:get_message(identifier) or nil
49+
return resource:get_message(identifier)
3250
end
3351

3452
function FluentBundle:add_messages (input, locale)
35-
if locale then self:set_locale(locale) end
53+
-- Work around Penlight #307
54+
-- self:_patch_init()
55+
locale = locale or self:get_locale()
3656
local syntax = FluentSyntax()
3757
local messages =
3858
type(input) == "string"
3959
and syntax:parsestring(input)
4060
or tablex.reduce('+', tablex.imap(function (v)
4161
return syntax:parsestring(v)
4262
end, input))
43-
self.locales[self.locale]:__add(messages)
44-
return self
63+
local resource = self:get_resource(locale)
64+
return resource + messages
4565
end
4666

4767
function FluentBundle:format (identifier, parameters)
48-
local resource = self.locales[self.locale]
68+
local resource = self:get_resource()
4969
local message = resource:get_message(identifier)
5070
return message:format(parameters)
5171
end

fluent/resource.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,21 @@ function FluentResource:_init (ast)
4242
end
4343
end
4444
flush()
45+
-- Work around Penlight #307
4546
-- self:catch(self.get_message)
47+
self:_patch_init()
48+
return self
49+
end
50+
51+
-- Work around Penlight #307
52+
function FluentResource:_patch_init ()
53+
if not type(rawget(getmetatable(self), "__index")) ~= "function" then
54+
self:catch(function(_, identifier) return self:get_message(identifier) end)
55+
end
4656
end
4757

4858
function FluentResource:load_node (node)
59+
self:_patch_init()
4960
local body = self.body
5061
local k = #body + 1
5162
body[k] = node
@@ -79,6 +90,11 @@ function FluentResource:get_message (identifier, isterm)
7990
return attribute and entry:get_attribute(attribute) or entry
8091
end
8192

93+
function FluentResource:format (identifier, parameters)
94+
local message = self:get_message(identifier)
95+
return message:format(parameters)
96+
end
97+
8298
function FluentResource:get_term (identifier)
8399
return self:get_message(identifier, true)
84100
end

fluent/syntax.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local FluentSyntax = class()
1111
-- luacheck: ignore 212
1212
function FluentSyntax:_init (input)
1313
-- TODO: handle file pointers, filnames, tables of pointers?
14+
return self
1415
end
1516

1617
function FluentSyntax:parsestring (input)

0 commit comments

Comments
 (0)