mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Add tool to sync translations
This commit is contained in:
parent
2c4d8e40fd
commit
6a588d23e5
@ -17,7 +17,8 @@
|
||||
"publishOnSteam": "cd gulp/steampipe && ./upload.bat",
|
||||
"publishStandalone": "yarn publishOnItch && yarn publishOnSteam",
|
||||
"publishWeb": "cd gulp && yarn main.deploy.prod",
|
||||
"publish": "yarn publishStandalone && yarn publishWeb"
|
||||
"publish": "yarn publishStandalone && yarn publishWeb",
|
||||
"syncTranslations": "node sync-translations.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.5.4",
|
||||
@ -45,6 +46,7 @@
|
||||
"logrocket": "^1.0.7",
|
||||
"lz-string": "^1.4.4",
|
||||
"markdown-loader": "^4.0.0",
|
||||
"match-all": "^1.2.5",
|
||||
"obfuscator-loader": "^1.1.2",
|
||||
"phonegap-plugin-mobile-accessibility": "^1.0.5",
|
||||
"promise-polyfill": "^8.1.0",
|
||||
@ -65,7 +67,9 @@
|
||||
"webpack-plugin-replace": "^1.1.1",
|
||||
"webpack-strip-block": "^0.2.0",
|
||||
"whatwg-fetch": "^3.0.0",
|
||||
"worker-loader": "^2.0.0"
|
||||
"worker-loader": "^2.0.0",
|
||||
"yaml": "^1.10.0",
|
||||
"yawn-yaml": "^1.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "3.0.1",
|
||||
|
87
sync-translations.js
Normal file
87
sync-translations.js
Normal file
@ -0,0 +1,87 @@
|
||||
// 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[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
|
||||
);
|
||||
console.warn(" | Obsolete key", path + key);
|
||||
// changes.push({
|
||||
// key: path + key,
|
||||
// value: null,
|
||||
// });
|
||||
delete translatedObj[key];
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
console.warn(" | Unknown type: ", typeof valueOriginal);
|
||||
}
|
||||
|
||||
// const matching = translatedObj[key];
|
||||
}
|
||||
|
||||
for (const key in translatedObj) {
|
||||
if (!originalObj[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");
|
||||
}
|
@ -45,4 +45,4 @@ PS: I'm super busy, but I'll give my best to do it quickly!
|
||||
|
||||
## Updating a language to the latest version
|
||||
|
||||
Right now there is no possibility to automatically update a translation to the latest version. It is required to manually check the base translation (`base-en.yaml`) and compare it to the other translations to remove unused keys and add new ones.
|
||||
Run `yarn syncTranslations` in the root directory to synchronize all translations to the latest version! This will remove obsolete keys and add newly added keys. (Run `yarn` before to install packes).
|
||||
|
34
yarn.lock
34
yarn.lock
@ -5316,6 +5316,14 @@ js-yaml@^3.13.1:
|
||||
argparse "^1.0.7"
|
||||
esprima "^4.0.0"
|
||||
|
||||
js-yaml@^3.4.2:
|
||||
version "3.14.0"
|
||||
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
|
||||
integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==
|
||||
dependencies:
|
||||
argparse "^1.0.7"
|
||||
esprima "^4.0.0"
|
||||
|
||||
jsesc@^2.5.1:
|
||||
version "2.5.2"
|
||||
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
|
||||
@ -5591,7 +5599,7 @@ lodash.uniq@^4.5.0:
|
||||
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
|
||||
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
|
||||
|
||||
lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.3.0:
|
||||
lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.3.0:
|
||||
version "4.17.15"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
|
||||
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
|
||||
@ -5726,6 +5734,11 @@ marked@^0.5.0:
|
||||
resolved "https://registry.yarnpkg.com/marked/-/marked-0.5.2.tgz#3efdb27b1fd0ecec4f5aba362bddcd18120e5ba9"
|
||||
integrity sha512-fdZvBa7/vSQIZCi4uuwo2N3q+7jJURpMVCcbaX0S1Mg65WZ5ilXvC67MviJAsdjqqgD+CEq4RKo5AYGgINkVAA==
|
||||
|
||||
match-all@^1.2.5:
|
||||
version "1.2.5"
|
||||
resolved "https://registry.yarnpkg.com/match-all/-/match-all-1.2.5.tgz#f709af311a7cb9ae464d9107a4f0fe08d3326eff"
|
||||
integrity sha512-KW4trRDMYbVkAKZ1J655vh0931mk3XM1lIJ480TXUL3KBrOsZ6WpryYJELonvtXC1O4erLYB069uHidLkswbjQ==
|
||||
|
||||
md5.js@^1.3.4:
|
||||
version "1.3.5"
|
||||
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
|
||||
@ -9753,6 +9766,16 @@ yallist@^3.0.2:
|
||||
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
|
||||
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==
|
||||
|
||||
yaml-js@^0.1.3:
|
||||
version "0.1.5"
|
||||
resolved "https://registry.yarnpkg.com/yaml-js/-/yaml-js-0.1.5.tgz#a01369010b3558d8aaed2394615dfd0780fd8fac"
|
||||
integrity sha1-oBNpAQs1WNiq7SOUYV39B4D9j6w=
|
||||
|
||||
yaml@^1.10.0:
|
||||
version "1.10.0"
|
||||
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
|
||||
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==
|
||||
|
||||
yargs-parser@^13.1.0:
|
||||
version "13.1.2"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-13.1.2.tgz#130f09702ebaeef2650d54ce6e3e5706f7a4fb38"
|
||||
@ -9857,6 +9880,15 @@ yauzl@^2.4.2:
|
||||
buffer-crc32 "~0.2.3"
|
||||
fd-slicer "~1.1.0"
|
||||
|
||||
yawn-yaml@^1.5.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/yawn-yaml/-/yawn-yaml-1.5.0.tgz#95fba7544d5375fce3dc84514f12218ed0d2ebcb"
|
||||
integrity sha512-sH2zX9K1QiWhWh9U19pye660qlzrEAd5c4ebw/6lqz17LZw7xYi7nqXlBoVLVtc2FZFXDKiJIsvVcKGYbLVyFQ==
|
||||
dependencies:
|
||||
js-yaml "^3.4.2"
|
||||
lodash "^4.17.11"
|
||||
yaml-js "^0.1.3"
|
||||
|
||||
yeast@0.1.2:
|
||||
version "0.1.2"
|
||||
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"
|
||||
|
Loading…
Reference in New Issue
Block a user