mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Update translation script
This commit is contained in:
parent
a0fb5b861c
commit
1a91963732
@ -1,82 +1,81 @@
|
||||
// Synchronizes all translations
|
||||
|
||||
const fs = require("fs");
|
||||
const matchAll = require("match-all");
|
||||
const path = require("path");
|
||||
const YAWN = require("yawn-yaml/cjs");
|
||||
const YAML = require("yaml");
|
||||
|
||||
const files = fs
|
||||
.readdirSync(path.join(__dirname, "translations"))
|
||||
.filter(x => x.endsWith(".yaml"))
|
||||
.filter(x => x.indexOf("base-en") < 0);
|
||||
|
||||
const originalContents = fs
|
||||
.readFileSync(path.join(__dirname, "translations", "base-en.yaml"))
|
||||
.toString("utf-8");
|
||||
|
||||
const original = YAML.parse(originalContents);
|
||||
|
||||
const placeholderRegexp = /[[<]([a-zA-Z_0-9]+)[\]<]/gi;
|
||||
|
||||
function match(originalObj, translatedObj, path = "/") {
|
||||
for (const key in originalObj) {
|
||||
if (!translatedObj.hasOwnProperty(key)) {
|
||||
console.warn(" | Missing key", path + key);
|
||||
translatedObj[key] = originalObj[key];
|
||||
continue;
|
||||
}
|
||||
const valueOriginal = originalObj[key];
|
||||
const valueMatching = translatedObj[key];
|
||||
if (typeof valueOriginal !== typeof valueMatching) {
|
||||
console.warn(" | MISMATCHING type (obj|non-obj) in", path + key);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof valueOriginal === "object") {
|
||||
match(valueOriginal, valueMatching, path + key + "/");
|
||||
} else if (typeof valueOriginal === "string") {
|
||||
// todo
|
||||
const originalPlaceholders = matchAll(valueOriginal, placeholderRegexp).toArray();
|
||||
const translatedPlaceholders = matchAll(valueMatching, placeholderRegexp).toArray();
|
||||
|
||||
if (originalPlaceholders.length !== translatedPlaceholders.length) {
|
||||
console.warn(
|
||||
" | Mismatching placeholders in",
|
||||
path + key,
|
||||
"->",
|
||||
originalPlaceholders,
|
||||
"vs",
|
||||
translatedPlaceholders
|
||||
);
|
||||
translatedObj[key] = originalObj[key];
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
console.warn(" | Unknown type: ", typeof valueOriginal);
|
||||
}
|
||||
|
||||
// const matching = translatedObj[key];
|
||||
}
|
||||
|
||||
for (const key in translatedObj) {
|
||||
if (!originalObj.hasOwnProperty(key)) {
|
||||
console.warn(" | Obsolete key", path + key);
|
||||
delete translatedObj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < files.length; ++i) {
|
||||
const filePath = path.join(__dirname, "translations", files[i]);
|
||||
console.log("Processing", files[i]);
|
||||
const translatedContents = fs.readFileSync(filePath).toString("utf-8");
|
||||
const translated = YAML.parse(translatedContents);
|
||||
const handle = new YAWN(translatedContents);
|
||||
|
||||
const json = handle.json;
|
||||
match(original, json, "/");
|
||||
handle.json = json;
|
||||
|
||||
fs.writeFileSync(filePath, handle.yaml, "utf-8");
|
||||
}
|
||||
// Synchronizes all translations
|
||||
|
||||
const fs = require("fs");
|
||||
const matchAll = require("match-all");
|
||||
const path = require("path");
|
||||
const YAML = require("yaml");
|
||||
|
||||
const files = fs
|
||||
.readdirSync(path.join(__dirname, "translations"))
|
||||
.filter(x => x.endsWith(".yaml"))
|
||||
.filter(x => x.indexOf("base-en") < 0);
|
||||
|
||||
const originalContents = fs
|
||||
.readFileSync(path.join(__dirname, "translations", "base-en.yaml"))
|
||||
.toString("utf-8");
|
||||
|
||||
const original = YAML.parse(originalContents);
|
||||
|
||||
const placeholderRegexp = /[[<]([a-zA-Z_0-9]+)[\]<]/gi;
|
||||
|
||||
function match(originalObj, translatedObj, path = "/") {
|
||||
for (const key in originalObj) {
|
||||
if (!translatedObj.hasOwnProperty(key)) {
|
||||
console.warn(" | Missing key", path + key);
|
||||
translatedObj[key] = originalObj[key];
|
||||
continue;
|
||||
}
|
||||
const valueOriginal = originalObj[key];
|
||||
const valueMatching = translatedObj[key];
|
||||
if (typeof valueOriginal !== typeof valueMatching) {
|
||||
console.warn(" | MISMATCHING type (obj|non-obj) in", path + key);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof valueOriginal === "object") {
|
||||
match(valueOriginal, valueMatching, path + key + "/");
|
||||
} else if (typeof valueOriginal === "string") {
|
||||
// todo
|
||||
const originalPlaceholders = matchAll(valueOriginal, placeholderRegexp).toArray();
|
||||
const translatedPlaceholders = matchAll(valueMatching, placeholderRegexp).toArray();
|
||||
|
||||
if (originalPlaceholders.length !== translatedPlaceholders.length) {
|
||||
console.warn(
|
||||
" | Mismatching placeholders in",
|
||||
path + key,
|
||||
"->",
|
||||
originalPlaceholders,
|
||||
"vs",
|
||||
translatedPlaceholders
|
||||
);
|
||||
translatedObj[key] = originalObj[key];
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
console.warn(" | Unknown type: ", typeof valueOriginal);
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in translatedObj) {
|
||||
if (!originalObj.hasOwnProperty(key)) {
|
||||
console.warn(" | Obsolete key", path + key);
|
||||
delete translatedObj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < files.length; ++i) {
|
||||
const filePath = path.join(__dirname, "translations", files[i]);
|
||||
console.log("Processing", files[i]);
|
||||
const translatedContents = fs.readFileSync(filePath).toString("utf-8");
|
||||
|
||||
const json = YAML.parse(translatedContents);
|
||||
match(original, json, "/");
|
||||
|
||||
const stringified = YAML.stringify(json, {
|
||||
indent: 4,
|
||||
simpleKeys: true,
|
||||
});
|
||||
fs.writeFileSync(filePath, stringified, "utf-8");
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user