2019-03-16 11:04:37 +00:00
|
|
|
--- Rewrites language files in order to be consistent with en_us.
|
|
|
|
--
|
|
|
|
-- This will take every given language file and rewrite it to be in the same
|
|
|
|
-- order as the en_us file. Any keys which appear in the given file, but not
|
|
|
|
-- in en_us are warned about.
|
|
|
|
--
|
|
|
|
-- When passing the `-v` flag, we also print any missing commands.
|
|
|
|
--
|
|
|
|
-- Note, this is not intended to be a fool-proof tool, rather a quick way to
|
|
|
|
-- ensure language files are mostly correct.
|
|
|
|
--
|
|
|
|
-- @example
|
|
|
|
--
|
|
|
|
-- # Reformat all files
|
|
|
|
-- > lua tools/language.lua
|
|
|
|
-- # Reformat German, and warn about missing entries
|
|
|
|
-- > lua tools/language.lua -v de_de
|
|
|
|
|
|
|
|
local primary = "en_us"
|
|
|
|
|
|
|
|
local secondary = {
|
|
|
|
"de_de",
|
2019-03-19 09:50:21 +00:00
|
|
|
"es_es",
|
2019-03-16 11:04:37 +00:00
|
|
|
"fr_fr",
|
|
|
|
"it_it",
|
|
|
|
"pt_br",
|
|
|
|
"sv_se",
|
|
|
|
}
|
|
|
|
|
|
|
|
local verbose = false
|
|
|
|
|
2019-03-31 09:22:33 +00:00
|
|
|
local path = "src/main/resources/assets/computercraft/lang/%s.json"
|
2019-03-16 11:04:37 +00:00
|
|
|
|
2019-03-31 09:22:33 +00:00
|
|
|
local args = { ... }
|
2019-03-16 11:04:37 +00:00
|
|
|
for i = #args, 1, -1 do
|
|
|
|
if args[i] == "-v" or args[i] == "--verbose" then
|
|
|
|
table.remove(args, i)
|
|
|
|
verbose = true
|
|
|
|
end
|
|
|
|
end
|
2019-03-31 09:22:33 +00:00
|
|
|
if #args > 0 then
|
|
|
|
secondary = args
|
|
|
|
end
|
2019-03-16 11:04:37 +00:00
|
|
|
|
|
|
|
-- Read the contents of the primary language file
|
|
|
|
local primary_contents, n = {}, 1
|
|
|
|
for line in io.lines(path:format(primary)) do
|
2019-03-31 09:22:33 +00:00
|
|
|
local key = line:match('^%s*"([^"]+)":.*$')
|
2019-03-16 11:04:37 +00:00
|
|
|
if key then
|
|
|
|
primary_contents[n], n = key, n + 1
|
2019-03-31 09:22:33 +00:00
|
|
|
elseif line == "" or line == "{" or line == "}" then
|
2019-03-16 11:04:37 +00:00
|
|
|
primary_contents[n], n = line, n + 1
|
|
|
|
else
|
|
|
|
io.stderr:write(("Unknown line %q in %s\n"):format(line, primary))
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, language in ipairs(secondary) do
|
|
|
|
local keys = {}
|
|
|
|
|
|
|
|
for line in io.lines(path:format(language)) do
|
2019-03-31 09:22:33 +00:00
|
|
|
local key, value = line:match('^%s*"([^"]+)":%s*(.-),?$')
|
2019-03-16 11:04:37 +00:00
|
|
|
if key then
|
|
|
|
if keys[key] then
|
|
|
|
io.stderr:write(("Duplicate keys for %q in %q\n"):format(key, language))
|
|
|
|
os.exit(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
keys[key] = value
|
2019-03-31 09:22:33 +00:00
|
|
|
elseif line ~= "" and line ~= "{" and line ~= "}" then
|
2019-03-16 11:04:37 +00:00
|
|
|
io.stderr:write(("Unknown line %q in %s\n"):format(line, language))
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local h = io.open(path:format(language), "wb")
|
2019-03-31 09:22:33 +00:00
|
|
|
h:write("{")
|
|
|
|
local has_blank, has_any = false, false
|
2019-03-16 11:04:37 +00:00
|
|
|
for _, line in ipairs(primary_contents) do
|
2019-03-31 09:22:33 +00:00
|
|
|
if line == "{" or line == "}" then
|
|
|
|
-- Skip
|
|
|
|
elseif line == "" then
|
|
|
|
has_blank = true
|
2019-03-16 11:04:37 +00:00
|
|
|
else
|
|
|
|
local translated = keys[line]
|
|
|
|
if translated then
|
2019-03-31 09:22:33 +00:00
|
|
|
if has_any then h:write(",") else has_any = true end
|
|
|
|
if has_blank then h:write("\n") has_blank = false end
|
|
|
|
|
|
|
|
h:write(("\n %q: %s"):format(line, translated))
|
2019-03-16 11:04:37 +00:00
|
|
|
keys[line] = nil
|
|
|
|
elseif verbose then
|
|
|
|
io.stderr:write(("Missing translation %q for %q\n"):format(line, language))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if next(keys) ~= nil then
|
|
|
|
local extra = {}
|
2019-03-31 09:22:33 +00:00
|
|
|
for k, v in pairs(keys) do
|
|
|
|
extra[#extra + 1] = ("\n %q: %s"):format(k, v)
|
|
|
|
end
|
2019-03-16 11:04:37 +00:00
|
|
|
table.sort(extra)
|
|
|
|
|
2019-03-19 09:50:21 +00:00
|
|
|
io.stderr:write(("%d additional unknown translation keys in %q\n"):format(#extra, language))
|
2019-03-16 11:04:37 +00:00
|
|
|
|
2019-03-31 09:22:33 +00:00
|
|
|
has_blank = true
|
|
|
|
|
|
|
|
for _, line in ipairs(extra) do
|
|
|
|
if has_any then h:write(",") else has_any = true end
|
|
|
|
if has_blank then h:write("\n") has_blank = false end
|
|
|
|
h:write(line)
|
|
|
|
end
|
2019-03-16 11:04:37 +00:00
|
|
|
end
|
|
|
|
|
2019-03-31 09:22:33 +00:00
|
|
|
h:write("\n}\n")
|
2019-03-16 11:04:37 +00:00
|
|
|
h:close()
|
|
|
|
end
|