-
Notifications
You must be signed in to change notification settings - Fork 1
/
removeIdenticalMsgidMsgstr.js
65 lines (55 loc) · 1.87 KB
/
removeIdenticalMsgidMsgstr.js
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
const fs = require('fs');
const path = require('path');
// Check if a filename was provided as an argument
if (process.argv.length < 3) {
console.error('Please provide the filename as an argument.');
process.exit(1);
}
// Get the filename from the command line arguments
const inputFile = process.argv[2];
// Check if the file exists
if (!fs.existsSync(inputFile)) {
console.error(`File not found: ${inputFile}`);
process.exit(1);
}
// Read the input file
fs.readFile(inputFile, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${err}`);
return;
}
// Split the file content into entries
const entries = data.split('\n\n');
// Function to extract multiline msgid or msgstr
function extractMultilineValue(entry, key) {
const lines = entry.split('\n');
const keyIndex = lines.findIndex(line => line.startsWith(key));
if (keyIndex === -1) return '';
let value = '';
for (let i = keyIndex + 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line.startsWith('"') && line.endsWith('"')) {
value += line.slice(1, -1);
} else {
break;
}
}
return value || lines[keyIndex].replace(`${key} `, '').replace(/^"(.*)"$/, '$1');
}
// Filter out entries where msgid and msgstr are identical
const filteredEntries = entries.filter(entry => {
const msgid = extractMultilineValue(entry, 'msgid');
const msgstr = extractMultilineValue(entry, 'msgstr');
return msgid !== msgstr;
});
// Join the filtered entries back into a single string
const outputData = filteredEntries.join('\n\n');
// Write the filtered content back to the original file
fs.writeFile(inputFile, outputData, 'utf8', (err) => {
if (err) {
console.error(`Error writing file: ${err}`);
} else {
console.log(`Filtered content has been written to ${inputFile}`);
}
});
});