forked from spring/spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_localizer.lua
More file actions
executable file
·136 lines (101 loc) · 3.51 KB
/
lua_localizer.lua
File metadata and controls
executable file
·136 lines (101 loc) · 3.51 KB
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
#!/usr/bin/env lua
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: lua_localizer.lua
-- brief: localizes some lua table entries for Spring widgets / gadgets
-- author: Dave Rodgers (aka: trepan)
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
require('io')
require('string')
require('table')
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local subTable = {
-- mixed, all caps
-- ['Script'] = { 'sc', 'SC_' }, -- na, don't do this one
['Spring'] = { 'sp', 'SP_' },
['gl'] = { 'gl', 'GL_' },
['GL'] = { 'GL', 'GL_' },
['CMD'] = { 'CMD_', 'CMD_' },
['CMDTYPE'] = { 'CMDTYPE_', 'CMDTYPE_' },
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local execName = arg[0]
local fileName = arg[1]
if (type(fileName) ~= 'string') then
print('Usage: ' .. execName .. ' <filename>')
end
local f, err = io.open(fileName, 'r')
if (f == nil) then
print(err)
return
end
local text = f:read('*a')
local function IsAllCaps(text)
return (string.find(text, "%l") == nil)
end
local localSubs = {}
for key, subs in pairs(subTable) do
local lower = subs[1]
local upper = subs[2]
local function Substitute(text, extra)
if (not extra) then
extra = ''
end
if (extra == '.') then
return text .. extra -- no substitution, nested tables
end
local call = string.sub(text, #key + 2)
local prefix = IsAllCaps(call) and upper or lower
local subText = prefix .. call
localSubs[subText] = text
return subText .. extra
end
text = string.gsub(text, '(' .. key .. '%.%a[%a%d_]*)(.)', Substitute)
end
-- sort the entries for printing, and find the max string length
local maxLen = 0
local sorted = {}
for lText, oText in pairs(localSubs) do
table.insert(sorted, { lText, oText })
maxLen = (maxLen > #lText) and maxLen or #lText
end
table.sort(sorted, function(a,b) return a[1] < b[1] end)
-- prepend the local definitions
local newText = ''
newText = newText .. string.rep('-', 80) .. '\n'
newText = newText .. string.rep('-', 80) .. '\n'
newText = newText .. '\n'
newText = newText .. '-- Automatically generated local definitions\n'
newText = newText .. '\n'
for _, newOld in ipairs(sorted) do
local fmt = 'local %-' .. maxLen .. 's = %s\n'
local def = string.format(fmt, newOld[1], newOld[2])
newText = newText .. def
print(string.sub(def, 1, -2))
end
newText = newText .. '\n'
newText = newText .. string.rep('-', 80) .. '\n'
newText = newText .. string.rep('-', 80) .. '\n'
newText = newText .. '\n'
newText = newText .. text
-- output the modified text
local outName = string.gsub(fileName, '.lua$', '.locals.lua')
local out, err = io.open(outName, 'w')
if (out == nil) then
print(err)
else
out:write(newText)
out:close()
print('\nSaved ' .. outName .. '\n')
end
f:close()
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------