Skip to content

Commit 209c36c

Browse files
committed
Check PO header for language, too
1 parent b60a76f commit 209c36c

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

po2json.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ for (const arg of process.argv.slice(2)) {
99
writePoFile(arg, outpath);
1010
}
1111

12+
function assertHeader(name, value) {
13+
if (!value) {
14+
throw new Error(`Bad .po file. "${name}" header is missing`);
15+
}
16+
}
17+
1218
function writePoFile(inpath, outpath) {
1319
const input = fs.readFileSync(inpath).toString().normalize();
1420
const poData = po.parse(input);
15-
21+
const pluralHeader =
22+
poData.headers['plural-forms'] || poData.headers['Plural-Forms'];
23+
const language = poData.headers.language || poData.headers.Language;
24+
assertHeader('Plural-Forms', pluralHeader);
25+
assertHeader('Language', language);
1626
const dict = {};
1727
for (const msg of Object.values(poData.translations[''])) {
1828
const msgid = msg.msgid;
@@ -23,21 +33,11 @@ function writePoFile(inpath, outpath) {
2333
}
2434
const compactPo = {
2535
headers: {
26-
'plural-forms': '',
27-
language: 'en',
28-
},
29-
contexts: {
30-
'': dict,
36+
'plural-forms': pluralHeader,
37+
language: language,
3138
},
39+
contexts: {'': dict},
3240
};
33-
const pluralHeader =
34-
poData.headers['plural-forms'] || poData.headers['Plural-Forms'];
35-
if (!pluralHeader) {
36-
throw new Error('Bad .po file. "Plural-Forms" header is missing ');
37-
}
38-
compactPo.headers['plural-forms'] = pluralHeader;
39-
compactPo.headers.language = poData.headers.language;
40-
4141
const outstream = fs.createWriteStream(outpath, {flags: 'w'});
4242
outstream.write('export default ');
4343
outstream.write(JSON.stringify(compactPo, null, 0));

0 commit comments

Comments
 (0)