1
0
mirror of https://github.com/minetest-mods/intllib.git synced 2024-11-15 06:50:29 +01:00
intllib/tools/findtext.lua

143 lines
2.4 KiB
Lua
Raw Normal View History

2013-02-27 07:27:51 +01:00
#! /usr/bin/env lua
2013-10-29 18:54:14 +01:00
local me = arg[0]:gsub(".*[/\\](.*)$", "%1")
local function err(fmt, ...)
io.stderr:write(("%s: %s\n"):format(me, fmt:format(...)))
os.exit(1)
end
local output
local inputs = { }
local lang
local author
local i = 1
local function usage()
print([[
Usage: ]]..me..[[ [OPTIONS] FILE...
Extract translatable strings from the given FILE(s).
Available options:
-h,--help Show this help screen and exit.
-o,--output X Set output file (default: stdout).
-a,--author X Set author.
-l,--lang X Set language name.
]])
2013-10-29 20:25:46 +01:00
os.exit(0)
2013-10-29 18:54:14 +01:00
end
while i <= #arg do
local a = arg[i]
if (a == "-h") or (a == "--help") then
usage()
elseif (a == "-o") or (a == "--output") then
i = i + 1
if i > #arg then
err("missing required argument to `%s'", a)
end
output = arg[i]
elseif (a == "-a") or (a == "--author") then
i = i + 1
if i > #arg then
err("missing required argument to `%s'", a)
end
author = arg[i]
elseif (a == "-l") or (a == "--lang") then
i = i + 1
if i > #arg then
err("missing required argument to `%s'", a)
end
lang = arg[i]
elseif a:sub(1, 1) ~= "-" then
table.insert(inputs, a)
else
err("unrecognized option `%s'", a)
end
i = i + 1
end
if #inputs == 0 then
err("no input files")
end
local outfile = io.stdout
local function printf(fmt, ...)
outfile:write(fmt:format(...))
2013-02-27 07:27:51 +01:00
end
2013-10-29 18:54:14 +01:00
if output then
local e
outfile, e = io.open(output, "w")
if not outfile then
err("error opening file for writing: %s", e)
end
2013-02-27 07:27:51 +01:00
end
2013-10-29 18:54:14 +01:00
if author or lang then
outfile:write("\n")
2013-02-27 07:27:51 +01:00
end
2013-10-29 18:54:14 +01:00
if lang then
printf("# Language: %s\n", lang)
2013-02-27 07:27:51 +01:00
end
2013-10-29 18:54:14 +01:00
if author then
printf("# Author: %s\n", author)
end
if author or lang then
outfile:write("\n")
end
local escapes = {
["\n"] = "\\n",
["="] = "\\=",
["\\"] = "\\\\",
}
local function escape(s)
return s:gsub("[\\\n=]", escapes)
end
local messages = { }
for _, file in ipairs(inputs) do
local infile, e = io.open(file, "r")
if infile then
for line in infile:lines() do
2015-02-02 00:10:09 +01:00
for s in line:gmatch('S%("([^"]*)"') do
2013-10-29 18:54:14 +01:00
table.insert(messages, s)
end
end
infile:close()
else
io.stderr:write(("%s: WARNING: error opening file: %s\n"):format(me, e))
end
end
table.sort(messages)
local last_msg
for i, msg in ipairs(messages) do
if msg ~= last_msg then
printf("%s =\n", escape(msg))
end
last_msg = msg
end
2013-02-27 07:27:51 +01:00
2013-10-29 18:54:14 +01:00
if output then
outfile:close()
2013-02-27 07:27:51 +01:00
end
2013-10-29 18:54:14 +01:00
--[[
TESTS:
S("foo") S("bar")
S("bar")
S("foo")
]]