1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-28 00:59:31 +00:00

Translation system first draft

This commit is contained in:
Étienne Fildadut 2023-12-23 21:09:12 +01:00
parent ffadc0dd69
commit c4636343b4
15 changed files with 215 additions and 18 deletions

49
ast/Translatable.lua Normal file
View file

@ -0,0 +1,49 @@
local ast = require("ast")
local TextInterpolation, String
local operator_priority = require("common").operator_priority
local translation_manager
local Translatable = ast.abstract.Node {
type = "translatable",
format_priority = operator_priority["%_"],
expression = nil,
init = function(self, expression)
self.expression = expression
self.context = ast.Struct:new()
self.context:set(String:new("source"), String:new(self.expression.source))
if TextInterpolation:is(self.expression) then
self.format_priority = expression.format_priority
end
end,
_format = function(self, ...)
if TextInterpolation:is(self.expression) then -- wrapped in translatable by default
return self.expression:format(...)
else
return "%"..self.expression:format_right(...)
end
end,
traverse = function(self, fn, ...)
fn(self.expression, ...)
end,
_eval = function(self, state)
return translation_manager:eval(state, self.context, self)
end,
list_translatable = function(self, t)
table.insert(t, self)
end
}
package.loaded[...] = Translatable
TextInterpolation, String = ast.TextInterpolation, ast.String
translation_manager = require("state.translation_manager")
return Translatable