forked from cockpit-project/cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest2po
executable file
·193 lines (167 loc) · 4.76 KB
/
manifest2po
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
/*
* Extracts translatable strings from manifest.json files.
*
*/
function fatal(message, code) {
console.log((filename || "manifest2po") + ": " + message);
process.exit(code || 1);
}
function usage() {
console.log("usage: manifest2po [-o output] input...");
process.exit(2);
}
var fs, path, stdio;
try {
fs = require('fs');
path = require('path');
stdio = require('stdio');
} catch (ex) {
fatal(ex.message, 127); /* missing looks for this */
}
var opts = stdio.getopt({
directory: { key: "d", args: 1, description: "Base directory for input files", default: "." },
output: { key: "o", args: 1, description: "Output file" },
from: { key: "f", args: 1, description: "File containing list of input files", default: "" },
});
if (!opts.from && opts.args.length < 1) {
usage();
}
var input = opts.args;
var entries = { };
/* Filename being parsed */
var filename = null;
prepare();
/* Decide what input files to process */
function prepare() {
if (opts.from) {
fs.readFile(opts.from, { encoding: "utf-8"}, function(err, data) {
if (err)
fatal(err.message);
input = data.split("\n").filter(function(value) {
return !!value;
}).concat(input);
step();
});
} else {
step();
}
}
/* Now process each file in turn */
function step() {
filename = input.shift();
if (filename === undefined) {
finish();
return;
}
if (path.basename(filename) != "manifest.json")
return step();
/* Qualify the filename if necessary */
var full = filename;
if (opts.directory)
full = path.join(opts.directory, filename);
fs.readFile(full, { encoding: "utf-8"}, function(err, data) {
if (err)
fatal(err.message);
// There are variables which when not substituted can cause JSON.parse to fail
// Dummy replace them. None variable is going to be translated anyway
safe_data = data.replace(/\@.+?\@/gi, 1);
process_manifest(JSON.parse(safe_data));
return step();
});
}
function process_manifest(manifest) {
if (manifest.menu)
process_menu(manifest.menu);
if (manifest.tools)
process_menu(manifest.tools);
}
function process_keywords(keywords) {
keywords.forEach(v => {
v.matches.forEach(keyword =>
push({
msgid: keyword,
locations: [ filename + ":0" ]
})
);
});
}
function process_docs(docs) {
docs.forEach(doc => {
push({
msgid: doc.label,
locations: [ filename + ":0" ]
})
});
}
function process_menu(menu) {
for (var m in menu) {
if (menu[m].label) {
push({
msgid: menu[m].label,
locations: [ filename + ":0" ]
});
}
if (menu[m].keywords)
process_keywords(menu[m].keywords);
if (menu[m].docs)
process_docs(menu[m].docs);
}
}
/* Push an entry onto the list */
function push(entry) {
var key = entry.msgid + "\0" + entry.msgid_plural + "\0" + entry.msgctxt;
var prev = entries[key];
if (prev) {
prev.locations = prev.locations.concat(entry.locations);
} else {
entries[key] = entry;
}
}
/* Escape a string for inclusion in po file */
function escape(string) {
var bs = string.split('\\').join('\\\\').split('"').join('\\"');
return bs.split("\n").map(function(line) {
return '"' + line + '"';
}).join("\n");
}
/* Finish by writing out the strings */
function finish() {
var result = [
'msgid ""',
'msgstr ""',
'"Project-Id-Version: PACKAGE_VERSION\\n"',
'"MIME-Version: 1.0\\n"',
'"Content-Type: text/plain; charset=UTF-8\\n"',
'"Content-Transfer-Encoding: 8bit\\n"',
'"X-Generator: Cockpit manifest2po\\n"',
'',
];
var msgid, entry;
for (msgid in entries) {
entry = entries[msgid];
result.push('#: ' + entry.locations.join(" "));
if (entry.msgctxt)
result.push('msgctxt ' + escape(entry.msgctxt));
result.push('msgid ' + escape(entry.msgid));
if (entry.msgid_plural) {
result.push('msgid_plural ' + escape(entry.msgid_plural));
result.push('msgstr[0] ""');
result.push('msgstr[1] ""');
} else {
result.push('msgstr ""');
}
result.push('');
}
var data = result.join('\n');
if (!opts.output) {
process.stdout.write(data);
process.exit(0);
} else {
fs.writeFile(opts.output, data, function(err) {
if (err)
fatal(err.message);
process.exit(0);
});
}
}