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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package.path = package.path .. ';script/?.lua;tools/?.lua'
local fs = require 'bee.filesystem'
local util = require 'utility'
local function getLongStringMark(text)
return text:match '^%[[=]*%['
end
local function isLongStringFinish(text, mark)
local emark = mark:gsub('%[', ']')
return util.stringEndWith(text, emark)
end
local function loadLocaleFile(filePath)
local fileContent = util.loadFile(filePath:string())
local data = {
map = {},
list = {},
}
if not fileContent then
return data
end
local current
local inLongString
for line, lineCount in util.eachLine(fileContent) do
if inLongString then
current.content[#current.content+1] = line
if isLongStringFinish(line, inLongString) then
inLongString = nil
end
goto CONTINUE
end
local comment = line:match '%s*%-%- TODO.+$'
if comment then
line = line:sub(1, - #comment - 1)
end
local key, space = line:match '^(.-)(%s*)=%s*$'
if key then
current = {
key = key,
line = lineCount,
space = space,
comment = comment,
content = {},
}
if not data.map[key] then
data.map[key] = current
data.list[#data.list+1] = current
end
elseif current then
if line:find '^%s*$' then
current = nil
else
current.content[#current.content+1] = line
inLongString = getLongStringMark(line)
if inLongString and isLongStringFinish(line, inLongString) then
inLongString = false
end
end
end
::CONTINUE::
end
return data
end
local localeNames = {}
for fullPath in fs.pairs(fs.path('locale')) do
localeNames[#localeNames+1] = fullPath:filename():string()
end
local function mergeList(allKeys, list)
local keyMap = {}
local leftFix = 0
for i, data in ipairs(list) do
local key = data.key
local leftIndex = i + leftFix
local left = allKeys[leftIndex]
if not left then
if not keyMap[key] then
keyMap[key] = true
allKeys[#allKeys+1] = key
end
goto CONTINUE
end
if left == key then
goto CONTINUE
end
for j = leftIndex + 1, #allKeys do
if allKeys[j] == key then
leftFix = j - i
goto CONTINUE
end
end
for j = i + 1, #list do
if list[j] == key then
leftFix = j - i
goto CONTINUE
end
end
if not keyMap[key] then
keyMap[key] = true
table.insert(allKeys, leftIndex, key)
end
::CONTINUE::
end
end
local function needSplit(key, lastKey)
if not lastKey then
return false
end
return key:match '%w+' ~= lastKey:match '%w+'
end
local function buildLocaleFile(localeName, allKeys, localeMap, fileName)
local lines = {}
local lastKey
local blocks = {}
local currentBlock = {}
blocks[#blocks+1] = currentBlock
for _, key in ipairs(allKeys) do
if needSplit(key, lastKey) then
currentBlock = {}
blocks[#blocks+1] = currentBlock
end
lastKey = key
currentBlock[#currentBlock+1] = key
end
if fileName == 'meta' then
lines[#lines+1] = '---@diagnostic disable: undefined-global, lowercase-global'
lines[#lines+1] = ''
end
if fileName == 'setting' then
lines[#lines+1] = '---@diagnostic disable: undefined-global'
lines[#lines+1] = ''
end
for _, block in ipairs(blocks) do
for _, key in ipairs(block) do
local data = localeMap[localeName].map[key]
local comment
if data then
local needTranslate = false
if localeName == 'en-us' then
local utfLen = 0
local charLen = 0
for _, line in ipairs(data.content) do
utfLen = utfLen + util.utf8Len(line)
charLen = charLen + #line
end
if charLen > 0 and utfLen / charLen < 0.8 then
needTranslate = true
end
else
--if #data.content > 1 or #data.content[1] > 2 then
-- local enContent = localeMap['en-us'].map[key] and localeMap['en-us'].map[key].content
-- if enContent then
-- needTranslate = #data.content == #enContent
-- for i, line in ipairs(data.content) do
-- if line ~= enContent[i] then
-- needTranslate = false
-- end
-- end
-- end
--end
end
if needTranslate then
comment = ' -- TODO: need translate!'
end
else
data = localeMap['en-us'].map[key] or localeMap['zh-cn'].map[key]
comment = ' -- TODO: need translate!'
end
lines[#lines+1] = key .. data.space .. '=' .. (comment or data.comment or '')
for _, line in ipairs(data.content) do
lines[#lines+1] = line
end
end
lines[#lines+1] = ''
end
return table.concat(lines, '\n')
end
local function processFile(fileName)
local allKeys = {}
local localeMap = {}
for _, localeName in ipairs(localeNames) do
local localeData = loadLocaleFile(fs.path('locale') / localeName / (fileName .. '.lua'))
localeMap[localeName] = localeData
mergeList(allKeys, localeData.list)
end
for _, localeName in ipairs(localeNames) do
local newContent = buildLocaleFile(localeName, allKeys, localeMap, fileName)
util.saveFile((fs.path('locale') / localeName / (fileName .. '.lua')):string(), newContent)
end
end
for _, fileName in ipairs { 'script', 'meta', 'setting' } do
processFile(fileName)
end
|