File tree Expand file tree Collapse file tree 3 files changed +26
-11
lines changed
Expand file tree Collapse file tree 3 files changed +26
-11
lines changed Original file line number Diff line number Diff 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
1919end
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+
2127function FluentBundle :set_locale (locale )
2228 self .locale = CLDR .locales [locale ] and locale or " und"
2329 if not self .locales [self .locale ] then
3238
3339function 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
3644end
3745
3846function FluentBundle :get_message (identifier )
@@ -42,6 +50,8 @@ function FluentBundle:get_message (identifier)
4250end
4351
4452function 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 =
Original file line number Diff line number Diff 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
4849end
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
Original file line number Diff line number Diff 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
245245end )
You can’t perform that action at this time.
0 commit comments