Import intllib 0.1.0
[minetest_tutorial_subgame.git] / mods / intllib / tools / updatetext.lua
blob00f9bf68db089b72e5f83552607fda35bbcfa37b
1 #! /usr/bin/env lua
3 local basedir = ""
4 if arg[0]:find("[/\\]") then
5 basedir = arg[0]:gsub("(.*[/\\]).*$", "%1"):gsub("\\", "/")
6 end
7 if basedir == "" then basedir = "./" end
9 -- Required by load_strings()
10 function string.trim(s)
11 return s:gsub("^%s*(.-)%s*$", "%1")
12 end
14 dofile(basedir.."/../lib.lua")
16 local me = arg[0]:gsub(".*[/\\](.*)$", "%1")
18 local function err(fmt, ...)
19 io.stderr:write(("%s: %s\n"):format(me, fmt:format(...)))
20 os.exit(1)
21 end
23 local template
24 local catalogs = { }
26 local function usage()
27 print([[
28 Usage: ]]..me..[[ [OPTIONS] TEMPLATE CATALOG...
30 Update a catalog with new strings from a template.
32 Available options:
33 -h,--help Show this help screen and exit.
34 -o,--output X Set output file (default: stdout).
36 Messages in the template that are not on the catalog are added to the
37 catalog at the end.
39 This tool also checks messages that are in the catalog but not in the
40 template, and reports such lines. It's up to the user to remove such
41 lines, if so desired.
42 ]])
43 os.exit(0)
44 end
46 local i = 1
48 while i <= #arg do
49 local a = arg[i]
50 if (a == "-h") or (a == "--help") then
51 usage()
52 elseif (a == "-o") or (a == "--output") then
53 i = i + 1
54 if i > #arg then
55 err("missing required argument to `%s'", a)
56 end
57 elseif (a == "-c") or (a == "--comment") then
58 old_msg_mode = "c"
59 elseif (a == "-d") or (a == "--delete") then
60 old_msg_mode = "d"
61 elseif a:sub(1, 1) ~= "-" then
62 if not template then
63 template = a
64 else
65 table.insert(catalogs, a)
66 end
67 else
68 err("unrecognized option `%s'", a)
69 end
70 i = i + 1
71 end
73 if not template then
74 err("no template specified")
75 elseif #catalogs == 0 then
76 err("no catalogs specified")
77 end
79 local f, e = io.open(template, "r")
80 if not f then
81 err("error opening template: %s", e)
82 end
84 local function printf(fmt, ...)
85 outfile:write(fmt:format(...))
86 end
88 local escapes = { ["\n"] = "\\n", ["="] = "\\=", ["\\"] = "\\\\", }
89 local function escape(s)
90 return s:gsub("[\\\n=]", escapes)
91 end
93 if output then
94 local e
95 outfile, e = io.open(output, "w")
96 if not outfile then
97 err("error opening file for writing: %s", e)
98 end
99 end
101 local function printf(fmt, ...)
102 io.stdout:write(fmt:format(...))
105 local template_msgs = intllib.load_strings(template)
107 for _, file in ipairs(catalogs) do
108 print("Processing: "..file)
109 local catalog_msgs = intllib.load_strings(file)
110 local dirty_lines = { }
111 if catalog_msgs then
112 -- Add new entries from template.
113 for k in pairs(template_msgs) do
114 if not catalog_msgs[k] then
115 print("NEW: "..k)
116 table.insert(dirty_lines, escape(k).." =")
119 -- Check for old messages.
120 for k, v in pairs(catalog_msgs) do
121 if not template_msgs[k] then
122 print("OLD: "..k)
125 if #dirty_lines > 0 then
126 local outf, e = io.open(file, "a+")
127 if outf then
128 outf:write("\n")
129 for _, line in ipairs(dirty_lines) do
130 outf:write(line)
131 outf:write("\n")
133 outf:close()
134 else
135 io.stderr:write(("%s: WARNING: cannot write: %s\n"):format(me, e))
138 else
139 io.stderr:write(("%s: WARNING: could not load catalog\n"):format(me))