|
| 1 | +local isOSX = os.getenv("MANPATH") and true or false |
| 2 | +local function output(msg) |
| 3 | + io.stdout:write(msg) |
| 4 | + io.stdout:write("\n") |
| 5 | + io.stdout:flush() |
| 6 | +end |
| 7 | + |
| 8 | +local file = io.open("localization.enUS.lua") |
| 9 | +local contents = file:read("*line") |
| 10 | +file:close() |
| 11 | + |
| 12 | +local LOCAL_VAR = string.match(contents, "^(.+) = {") |
| 13 | +if( not LOCAL_VAR ) then |
| 14 | + output("Failed to find localization variable in localization.enUS.lua") |
| 15 | + return |
| 16 | +else |
| 17 | + output("Localization key " .. LOCAL_VAR) |
| 18 | +end |
| 19 | + |
| 20 | +function string.trim(text) |
| 21 | + return string.gsub(text, "^%s*(.-)%s*$", "%1") |
| 22 | +end |
| 23 | + |
| 24 | +local foundLocals = {} |
| 25 | +local totalFound = 0 |
| 26 | +local function scanFile(path) |
| 27 | + output("Scanning " .. path) |
| 28 | + |
| 29 | + local contents = io.open(path):read("*all") |
| 30 | + for line in string.gmatch(contents, "L%[\"(.-)\"%]") do |
| 31 | + foundLocals[string.trim(line)] = true |
| 32 | + totalFound = totalFound + 1 |
| 33 | + end |
| 34 | + |
| 35 | + for line in string.gmatch(contents, LOCAL_VAR .. "%[\"(.-)\"%]") do |
| 36 | + foundLocals[string.trim(line)] = true |
| 37 | + totalFound = totalFound + 1 |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +local function scanFolder(path) |
| 42 | + local command |
| 43 | + if( isOSX ) then |
| 44 | + command = string.format("ls \"%s\"", (path or "./")) |
| 45 | + else |
| 46 | + command = string.format("dir /B \"%s\"", (path or "")) |
| 47 | + end |
| 48 | + |
| 49 | + for file in io.popen(command):lines() do |
| 50 | + local extension = string.match(file, "%.([%a%d]+)$") |
| 51 | + if( file ~= "" and not extension and file ~= "libs" ) then |
| 52 | + if( path ) then |
| 53 | + scanFolder(path .. "/" .. file) |
| 54 | + else |
| 55 | + scanFolder(file) |
| 56 | + end |
| 57 | + elseif( extension == "lua" and file ~= "localcheck.lua" and file ~= "globalcheck.lua" and not string.match(file, "^localization") ) then |
| 58 | + if( path ) then |
| 59 | + scanFile(path .. "/" .. file) |
| 60 | + else |
| 61 | + scanFile(file) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +scanFolder() |
| 68 | + |
| 69 | +output("Total keys found " .. totalFound) |
| 70 | + |
| 71 | +-- Sort everything |
| 72 | +local keyOrder = {} |
| 73 | +for key in pairs(foundLocals) do |
| 74 | + table.insert(keyOrder, key) |
| 75 | +end |
| 76 | + |
| 77 | +table.sort(keyOrder, function(a, b) return a < b end) |
| 78 | + |
| 79 | +-- Load the current localization to get the tables out |
| 80 | +dofile("localization.enUS.lua") |
| 81 | + |
| 82 | +local file = io.open("localization.enUS.lua", "w") |
| 83 | +file:write(LOCAL_VAR .. " = {") |
| 84 | + |
| 85 | +-- Write all used keys |
| 86 | +for _, key in pairs(keyOrder) do |
| 87 | + file:write(string.format("\n [\"%s\"] = \"%s\",", key, key)) |
| 88 | +end |
| 89 | + |
| 90 | +-- Format the string so it can be written |
| 91 | +local function parse(text) |
| 92 | + text = string.gsub(text, "\n", "\\n") |
| 93 | + text = string.gsub(text, "\"", "\\\"") |
| 94 | + text = string.trim(text) |
| 95 | + |
| 96 | + return text |
| 97 | +end |
| 98 | + |
| 99 | +-- Tables inside localization are assumed to always be there |
| 100 | +local _G = getfenv(0) |
| 101 | +local keyOrder = {} |
| 102 | +for key, data in pairs(_G[LOCAL_VAR]) do |
| 103 | + if( type(data) == "table" ) then |
| 104 | + table.insert(keyOrder, key) |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +table.sort(keyOrder, function(a, b) return a < b end) |
| 109 | + |
| 110 | +file:write("\n") |
| 111 | + |
| 112 | +local hadTables |
| 113 | +local function writeTable(key, tbl, depth) |
| 114 | + file:write(string.format(string.rep(" ", depth) .. "[\"%s\"] = {\n", key)) |
| 115 | + |
| 116 | + local data = tbl[key] |
| 117 | + local subKeyOrder = {} |
| 118 | + for subKey in pairs(data) do |
| 119 | + table.insert(subKeyOrder, subKey) |
| 120 | + end |
| 121 | + |
| 122 | + table.sort(subKeyOrder, function(a, b) return a < b end) |
| 123 | + for _, subKey in pairs(subKeyOrder) do |
| 124 | + if( type(data[subKey]) == "table" ) then |
| 125 | + writeTable(subKey, data, depth + 1) |
| 126 | + elseif( tonumber(subKey) ) then |
| 127 | + file:write(string.format("%s[%s] = \"%s\",\n", string.rep(" ", depth + 1), subKey, parse(data[subKey]))) |
| 128 | + else |
| 129 | + file:write(string.format("%s[\"%s\"] = \"%s\",\n", string.rep(" ", depth + 1), parse(subKey), parse(data[subKey]))) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + hadTables = true |
| 134 | + file:write(string.rep(" ", depth) .. "},\n") |
| 135 | +end |
| 136 | + |
| 137 | +for _, key in pairs(keyOrder) do |
| 138 | + writeTable(key, _G[LOCAL_VAR], 1) |
| 139 | +end |
| 140 | + |
| 141 | +if( hadTables ) then |
| 142 | + file:write("\n") |
| 143 | +end |
| 144 | + |
| 145 | +file:write("}") |
| 146 | +file:flush() |
| 147 | +file:close() |
| 148 | + |
| 149 | + |
| 150 | +output("Done") |
| 151 | + |
| 152 | + |
| 153 | + |
0 commit comments