Skip to content

Commit 1d52f1b

Browse files
committed
Work around new incarnation of Penlight №307 and (commented) test for new fail
1 parent 85e18a2 commit 1d52f1b

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

fluent/init.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ function FluentBundle:_init (locale)
1414
self:set_locale(locale)
1515
-- Work around Penlight #307
1616
-- self:catch(self.get_message)
17-
self:catch(function(_, identifier) return self:get_message(identifier) end)
17+
self:_patch_init()
1818
return self
1919
end
2020

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
25+
end
26+
2127
function FluentBundle:set_locale (locale)
2228
self.locale = CLDR.locales[locale] and locale or "und"
2329
if not self.locales[self.locale] then
@@ -32,7 +38,9 @@ end
3238

3339
function FluentBundle:get_resource (locale)
3440
local locales = self.locales
35-
return locales[locale or self:get_locale()]
41+
local resource = locales[locale or self:get_locale()]
42+
resource._patch_init(resource)
43+
return resource
3644
end
3745

3846
function FluentBundle:get_message (identifier)
@@ -42,6 +50,8 @@ function FluentBundle:get_message (identifier)
4250
end
4351

4452
function FluentBundle:add_messages (input, locale)
53+
-- Work around Penlight #307
54+
-- self:_patch_init()
4555
locale = locale or self:get_locale()
4656
local syntax = FluentSyntax()
4757
local messages =

fluent/resource.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ function FluentResource:_init (ast)
4444
flush()
4545
-- Work around Penlight #307
4646
-- self:catch(self.get_message)
47+
self:_patch_init()
4748
return self
4849
end
4950

50-
function FluentResource:load_node (node)
51-
-- Work around Penlight #307
51+
-- Work around Penlight #307
52+
function FluentResource:_patch_init ()
5253
if not type(rawget(getmetatable(self), "__index")) ~= "function" then
5354
self:catch(function(_, identifier) return self:get_message(identifier) end)
5455
end
56+
end
57+
58+
function FluentResource:load_node (node)
59+
self:_patch_init()
5560
local body = self.body
5661
local k = #body + 1
5762
body[k] = node

spec/fluent_spec.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,17 @@ foo =
229229
bundle:add_messages("hi = hello")
230230
assert.same("hello", bundle:format("hi"))
231231
bundle:set_locale("tr")
232-
bundle:add_messages("hi = merhaba")
233-
assert.same("merhaba", bundle:format("hi"))
232+
bundle:add_messages("hi = merhaba2")
233+
assert.same("merhaba2", bundle:format("hi"))
234234
bundle:set_locale("en")
235-
bundle:add_messages("hi = hello")
235+
bundle:add_messages("hi = hello2")
236236
local en = bundle:get_resource("en")
237237
local tr = bundle:get_resource("tr")
238-
assert.same("merhaba", tr:format("hi"))
239-
assert.same("hello", en:format("hi"))
238+
assert.same("merhaba2", tr:format("hi"))
239+
assert.same("hello2", en:format("hi"))
240240
-- TODO Penlight hack doesn't work around this?
241-
-- assert.same("merhaba", tr.hi())
242-
-- assert.same("hello", en.hi())
241+
assert.same("merhaba2", tr.hi())
242+
-- assert.same("hello2", en.hi())
243243
end)
244244

245245
end)

0 commit comments

Comments
 (0)