4 if arg
[0]:find("[/\\]") then
5 basedir
= arg
[0]:gsub("(.*[/\\]).*$", "%1"):gsub("\\", "/")
7 if basedir
== "" then basedir
= "./" end
9 -- Required by load_strings()
10 function string.trim(s
)
11 return s
:gsub("^%s*(.-)%s*$", "%1")
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(...)))
26 local function usage()
28 Usage: ]]..me
..[[ [OPTIONS] TEMPLATE CATALOG...
30 Update a catalog with new strings from a template.
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
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
50 if (a
== "-h") or (a
== "--help") then
52 elseif (a
== "-o") or (a
== "--output") then
55 err("missing required argument to `%s'", a
)
57 elseif (a
== "-c") or (a
== "--comment") then
59 elseif (a
== "-d") or (a
== "--delete") then
61 elseif a
:sub(1, 1) ~= "-" then
65 table.insert(catalogs
, a
)
68 err("unrecognized option `%s'", a
)
74 err("no template specified")
75 elseif #catalogs
== 0 then
76 err("no catalogs specified")
79 local f
, e
= io
.open(template
, "r")
81 err("error opening template: %s", e
)
84 local function printf(fmt
, ...)
85 outfile
:write(fmt
:format(...))
88 local escapes
= { ["\n"] = "\\n", ["="] = "\\=", ["\\"] = "\\\\", }
89 local function escape(s
)
90 return s
:gsub("[\\\n=]", escapes
)
95 outfile
, e
= io
.open(output
, "w")
97 err("error opening file for writing: %s", e
)
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
= { }
112 -- Add new entries from template.
113 for k
in pairs(template_msgs
) do
114 if not catalog_msgs
[k
] then
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
125 if #dirty_lines
> 0 then
126 local outf
, e
= io
.open(file
, "a+")
129 for _
, line
in ipairs(dirty_lines
) do
135 io
.stderr
:write(("%s: WARNING: cannot write: %s\n"):format(me
, e
))
139 io
.stderr
:write(("%s: WARNING: could not load catalog\n"):format(me
))