forked from Mogara/QSanguosha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts2lua.lua
57 lines (47 loc) · 1.29 KB
/
ts2lua.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- This is a script that extract translation informatio
-- from TS file to serveral translation files with lua format
-- The script requires 'lxp' which is the expat binding for Lua
require "lxp.lom"
content = io.open("sanguosha.ts"):read("*a")
content = lxp.lom.parse(content)
function parse_message(message)
local source, translation
for _, element in ipairs(message) do
if element.tag == "source" then
source = element[1]
elseif element.tag == "translation" then
translation = element[1]
end
end
return { source, translation }
end
function parse_context(context)
local name
local messages = {}
for _, element in ipairs(context) do
if element.tag == "name" then
name = element[1]
elseif element.tag == "message" then
table.insert(messages, parse_message(element))
end
end
if name then
local f = io.open(name .. ".lua", "w")
f:write("-- translation for " .. name .. "\n\n")
f:write("return {\n")
for _, message in ipairs(messages) do
local source = message[1]
local translation = message[2]
if source and translation then
f:write(("\t[%q] = %q, \n"):format(source, translation))
end
end
f:write("}\n")
f:close()
end
end
for _, context in ipairs(content) do
if context.tag == "context" then
parse_context(context)
end
end