-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrollback.ts
More file actions
104 lines (87 loc) · 2.91 KB
/
rollback.ts
File metadata and controls
104 lines (87 loc) · 2.91 KB
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
import * as fs from "fs";
import * as path from "path";
import gettextParser = require("gettext-parser");
interface TranslationItem {
msgid: string;
msgstr: string[];
}
interface Translations {
[context: string]: {
[msgid: string]: TranslationItem;
};
}
interface PoData {
translations: Translations;
}
// Function to check if a msgid is a header entry
function isHeaderEntry(msgid: string): boolean {
return (
msgid === "" ||
msgid.includes("Project-Id-Version") ||
msgid.includes("Report-Msgid-Bugs-To")
);
}
// Read PO files
const poFilePath = path.join(__dirname, "chinese", "zh-Hans.po");
const backupFilePath = path.join(__dirname, "chinese", "zh-Hans.bk.po");
// Check if both files exist
if (!fs.existsSync(poFilePath)) {
console.error(`Error: Main PO file not found: ${poFilePath}`);
process.exit(1);
}
if (!fs.existsSync(backupFilePath)) {
console.error(`Error: Backup PO file not found: ${backupFilePath}`);
process.exit(1);
}
const poContent = fs.readFileSync(poFilePath, "utf-8");
const backupContent = fs.readFileSync(backupFilePath, "utf-8");
// Parse PO files
const poData = gettextParser.po.parse(poContent) as PoData;
const backupData = gettextParser.po.parse(backupContent) as PoData;
// Create a map of backup translations
const backupTranslations = new Map<string, string>();
for (const [context, translations] of Object.entries(backupData.translations)) {
for (const [msgid, item] of Object.entries(translations)) {
if (item.msgstr && item.msgstr[0] && !isHeaderEntry(msgid)) {
backupTranslations.set(msgid, item.msgstr[0]);
}
}
}
// Process translations and rollback empty values
let rolledBackCount = 0;
let skippedCount = 0;
let totalEntries = 0;
for (const [context, translations] of Object.entries(poData.translations)) {
for (const [msgid, item] of Object.entries(translations)) {
// Skip header entries
if (isHeaderEntry(msgid)) {
skippedCount++;
continue;
}
totalEntries++;
// Check if the value is empty or "none"
if (
!item.msgstr[0] ||
item.msgstr[0].trim() === "" ||
item.msgstr[0].toLowerCase() === "none"
) {
// If backup exists, replace with backup
if (backupTranslations.has(msgid)) {
const backupValue = backupTranslations.get(msgid)!;
console.log(`Rolling back: ${msgid}`);
console.log(` From: ${item.msgstr[0] || "(empty)"}`);
console.log(` To: ${backupValue}\n`);
item.msgstr = [backupValue];
rolledBackCount++;
}
}
}
}
// Save updated PO file
const updatedPoContent = gettextParser.po.compile(poData);
fs.writeFileSync(poFilePath, updatedPoContent);
console.log("\nRollback Summary:");
console.log(`Total entries processed: ${totalEntries}`);
console.log(`Skipped header entries: ${skippedCount}`);
console.log(`Rolled back entries: ${rolledBackCount}`);
console.log(`\nRollback completed! File updated: chinese/zh-Hans.po`);