-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgenerate-the-other-languages-doc.js
210 lines (183 loc) · 7.39 KB
/
generate-the-other-languages-doc.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const assert = require("assert");
const fs = require("fs");
const { parseArgs } = require("util");
require("dotenv").config();
var docFolder = "./src/data/";
var docFiles = ["actions.ts", "constants.ts", "keywords.ts", "stringKw.ts", "values.ts"];
var datatoolPath = process.env.DATATOOL_PATH ?? "%USERPROFILE%\\toolchain-release\\DataTool.exe";
var overwatchPath = process.env.OVERWATCH_PATH ?? "C:\\Program Files (x86)\\Overwatch";
var outputFolder = "strings";
var guids;
var guidToLocaleMap = new Map();
var enUSToGuidMap = new Map();
var enUSFuzzyToGuidMap = new Map();
var removeParentheses = true;
var fuzzyMatch = false;
const args = parseArgs({
options: {
regenerateStringsFile: {
type: "boolean",
default: false,
},
},
});
async function generateStringsFile() {
const { execSync } = require("child_process");
let command = '"' + datatoolPath + '" "' + overwatchPath + '" dump-all-locale-strings --out=' + outputFolder + "/strings.json";
console.log("Extracting all locale strings with DataTool...");
execSync(command, (err, stdout, stderr) => {
if (err) {
console.log("DataTool failed with error:", err);
return;
}
});
}
function getGuids() {
console.log("Generating GUID mappings...");
guids = JSON.parse(fs.readFileSync(outputFolder + "/strings.json"));
// Precompute some mappings to save time later at cost of space complexity
for (let guidGlob of Object.entries(guids)) {
// New DataTool versions sometimes give empty objects or null for some GUIDs, skip them.
if (guidGlob[1] === null || objectIsEmpty(guidGlob[1])) continue;
let guid = guidGlob[0].split(".")[0];
guidToLocaleMap.set(guid, guidGlob[1]);
assert(Object.keys(guidGlob[1]).includes("enUS"), "enUS not found in guid " + guid);
let enUSKey = guidGlob[1]["enUS"];
if (enUSToGuidMap.has(enUSKey)) {
// console.debug("Duplicate enUS key found: "+enUSKey);
continue;
}
enUSToGuidMap.set(guidGlob[1]["enUS"], guid);
enUSFuzzyToGuidMap.set(guidGlob[1]["enUS"].replace(/[\.,;'\s()-]/g, "").toLowerCase(), guid);
}
//console.log(guids["en-US"]);
}
function objectIsEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) return false;
}
return true;
}
function replaceJsonObjectsInFile(path) {
console.log("Processing " + path);
let content = "" + fs.readFileSync(path);
let result = "";
let currentJsonStr = "";
let isInJsonObject = false;
for (let line of content.split(/\r?\n/g)) {
if (line.trim() === "//end-json") {
isInJsonObject = false;
tmpObj = iterateOnObject(eval("(" + currentJsonStr + ")"));
result += JSON.stringify(tmpObj, null, 4) + "\n";
currentJsonStr = "";
}
if (!isInJsonObject) {
result += line + "\n";
} else {
currentJsonStr += line + "\n";
}
if (line.trim() === "//begin-json") {
isInJsonObject = true;
}
}
fs.writeFileSync(path, result.substring(0, result.length - 1));
}
function iterateOnObject(content) {
if (content.onlyInOw1) {
return content;
}
if ("guid" in content || "en-US" in content) {
content = addTranslations(content);
}
/*if ("description" in content && !("descriptionLocalized" in content)) {
content["descriptionLocalized"] = {"en-US": content["description"]}
}*/
for (var key of Object.keys(content)) {
if (typeof content[key] === "object" && content[key] !== null) {
//Skip the comparison operators as they must not be translated.
if (key !== "__Operator__" && key !== "descriptionLocalized") {
if (key === "nameLocalized" || key === "descriptionLocalized") {
oldRemoveParentheses = removeParentheses;
removeParentheses = false;
fuzzyMatch = true;
} else {
fuzzyMatch = false;
}
content[key] = iterateOnObject(content[key]);
if (key === "nameLocalized" || key === "descriptionLocalized") {
removeParentheses = oldRemoveParentheses;
}
}
}
}
return content;
}
function addTranslations(content) {
if (!("guid" in content) || content.guid === "<unknown guid>") {
assert(Object.keys(content).includes("en-US"), "GUID-less content does not have an en-US key: " + JSON.stringify(content));
if (fuzzyMatch) {
content.guid = enUSFuzzyToGuidMap.get(content["en-US"].replace(/[\.,;'\s()-]/g, "").toLowerCase());
} else {
content.guid = enUSToGuidMap.get(content["en-US"]);
}
}
if (content.guid === undefined) {
console.warn("GUID not found for content: " + JSON.stringify(content));
return content;
}
let guidGlob = guidToLocaleMap.get(content.guid);
if (!guidGlob) {
console.warn(`GUID ${content.guid} for ${JSON.stringify(content)} appears to have become invalid! Attempting to rectify by finding the GUID again...`);
if (fuzzyMatch) {
content.guid = enUSFuzzyToGuidMap.get(content["en-US"].replace(/[\.,;'\s()-]/g, "").toLowerCase());
} else {
content.guid = enUSToGuidMap.get(content["en-US"]);
}
guidGlob = guidToLocaleMap.get(content.guid);
if (!guidGlob) {
console.error(`No valid GUID found for content ${JSON.stringify(content)}`);
return content;
}
console.log(`New GUID found: ${content.guid}, proceeding...`);
}
for (let localeEntry of Object.entries(guidGlob)) {
localeEntry[1] = localeEntry[1].replace(/%%/g, "%");
if (removeParentheses) localeEntry[1] = localeEntry[1].replace(/[,\(\)\/]/g, "");
content[dataToolLocaleToOverPyLocale(localeEntry[0])] = localeEntry[1];
}
return content;
}
function normalizeName(content) {
content = content.replace(/(?<=[a-z])(?=[A-Z0-9])/g, " ");
content = content.replace("-", " - ");
content = content.replace(":", ": ");
content = content.replace(/\s+/g, " ");
var words = content.split(" ");
words = words.map((x) => x[0].toUpperCase() + x.substring(1).toLowerCase()).join(" ");
console.log(words);
content = words;
return content;
}
if (args.values.regenerateStringsFile) generateStringsFile();
getGuids();
replaceJsonObjectsInFile(docFolder + "actions.ts");
replaceJsonObjectsInFile(docFolder + "values.ts");
replaceJsonObjectsInFile(docFolder + "constants.ts");
replaceJsonObjectsInFile(docFolder + "heroes.ts");
replaceJsonObjectsInFile(docFolder + "maps.ts");
replaceJsonObjectsInFile(docFolder + "gamemodes.ts");
replaceJsonObjectsInFile(docFolder + "customGameSettings.ts");
removeParentheses = false;
replaceJsonObjectsInFile(docFolder + "ui.ts");
replaceJsonObjectsInFile(docFolder + "argnames.ts");
replaceJsonObjectsInFile(docFolder + "localizedStrings.ts");
replaceJsonObjectsInFile(docFolder + "other.ts");
/**
* Converts a locale from the DataTool format to the OverPy format.
* @param {string} locale The locale to convert (e.g. "enUS")
* @returns {string} The converted locale (e.g. "en-US")
*/
function dataToolLocaleToOverPyLocale(locale) {
let result = locale.match(/([a-z]+)([A-Z]+)/);
return result[1] + "-" + result[2];
}