Skip to content

Commit 24f6565

Browse files
committed
Refactor syntax for Penlight classes
1 parent ad4760d commit 24f6565

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

fluent/init.lua

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,46 @@ local CLDR = require("cldr")
77
local FluentSyntax = require("fluent.syntax")
88
local FluentResource = require("fluent.resource")
99

10-
local FluentBundle = class({
11-
_init = function (self, locale)
12-
self.locales = {}
13-
self:set_locale(locale)
14-
-- Penlight bug #307, should be — self:catch(self.get_message)
15-
self:catch(function(_, identifier) return self:get_message(identifier) end)
16-
end,
17-
18-
set_locale = function (self, locale)
19-
self.locale = CLDR.locales[locale] and locale or "und"
20-
if not self.locales[self.locale] then
21-
self.locales[self.locale] = FluentResource()
22-
end
23-
end,
24-
25-
get_message = function (self, identifier)
26-
local locales = rawget(self, "locales")
27-
-- TODO iterate over fallback locales if not found in current one
28-
local resource = rawget(locales, self.locale)
29-
return resource:get_message(identifier) or nil
30-
end,
31-
32-
add_messages = function (self, input, locale)
33-
if locale then self:set_locale(locale) end
34-
local syntax = FluentSyntax()
35-
local messages =
36-
type(input) == "string"
37-
and syntax:parsestring(input)
38-
or tablex.reduce('+', tablex.imap(function (v)
39-
return syntax:parsestring(v)
40-
end, input))
41-
self.locales[self.locale]:__add(messages)
42-
return self
43-
end,
44-
45-
format = function (self, identifier, parameters)
46-
local resource = self.locales[self.locale]
47-
local message = resource:get_message(identifier)
48-
-- local message = resource[identifier]
49-
return message:format(parameters)
50-
end
51-
})
10+
local FluentBundle = class()
11+
FluentBundle.locales = {}
12+
13+
function FluentBundle:_init (locale)
14+
self:set_locale(locale)
15+
-- Penlight bug #307, should be — self:catch(self.get_message)
16+
self:catch(function(_, identifier) return self:get_message(identifier) end)
17+
end
18+
19+
function FluentBundle:set_locale (locale)
20+
self.locale = CLDR.locales[locale] and locale or "und"
21+
if not self.locales[self.locale] then
22+
self.locales[self.locale] = FluentResource()
23+
end
24+
end
25+
26+
function FluentBundle:get_message (identifier)
27+
local locales = rawget(self, "locales")
28+
-- TODO iterate over fallback locales if not found in current one
29+
local resource = rawget(locales, self.locale)
30+
return resource:get_message(identifier) or nil
31+
end
32+
33+
function FluentBundle:add_messages (input, locale)
34+
if locale then self:set_locale(locale) end
35+
local syntax = FluentSyntax()
36+
local messages =
37+
type(input) == "string"
38+
and syntax:parsestring(input)
39+
or tablex.reduce('+', tablex.imap(function (v)
40+
return syntax:parsestring(v)
41+
end, input))
42+
self.locales[self.locale]:__add(messages)
43+
return self
44+
end
45+
46+
function FluentBundle:format (identifier, parameters)
47+
local resource = self.locales[self.locale]
48+
local message = resource:get_message(identifier)
49+
return message:format(parameters)
50+
end
5251

5352
return FluentBundle

0 commit comments

Comments
 (0)