1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/.tools/translate.js

92 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2018-09-12 00:07:39 +02:00
const fs = require("fs");
const path = require("path");
const util = require("util");
const en = require("../_locales/en/messages.json");
const enKeys = Object.keys(en);
const language = process.argv[2];
function getTranslationPath(language){
2019-11-30 02:05:37 +01:00
"use strict";
2018-09-12 00:07:39 +02:00
return path.join(__dirname, "../_locales/" + language + "/messages.json");
}
async function loadTranslation(language){
2019-11-30 02:05:37 +01:00
"use strict";
2018-09-12 00:07:39 +02:00
const path = getTranslationPath(language);
2019-11-30 02:05:37 +01:00
const exists = await util.promisify(fs.exists)(path);
if (exists){
console.log("language exists -> load data");
const data = await util.promisify(fs.readFile)(path, {encoding: "UTF-8"});
return JSON.parse(data);
}
else {
console.log("language does not exist -> create it");
return {};
}
2018-09-12 00:07:39 +02:00
}
async function saveTranslation(language, data){
2019-11-30 02:05:37 +01:00
"use strict";
2018-09-12 00:07:39 +02:00
const path = getTranslationPath(language);
return await util.promisify(fs.writeFile)(path, JSON.stringify(data, null, "\t"));
}
async function getInput(prompt){
2019-11-30 02:05:37 +01:00
"use strict";
return new Promise(function(resolve){
2018-09-12 00:07:39 +02:00
process.stdout.write(prompt);
2019-11-30 02:05:37 +01:00
process.stdin.setEncoding("utf8");
2018-09-12 00:07:39 +02:00
process.stdin.resume();
process.stdin.on("data", function onData(data){
process.stdin.removeListener("data", onData);
process.stdin.pause();
resolve(data.replace(/[\n\r]+$/, ""));
});
});
}
async function askForTranslation(key){
2019-11-30 02:05:37 +01:00
"use strict";
2018-09-12 00:07:39 +02:00
const enData = en[key];
console.log("English translation for", key, ":", enData.message);
if (enData.description){
console.log("\nDescription:", enData.description);
}
2019-11-30 02:05:37 +01:00
return getInput("Please enter translation: ");
2018-09-12 00:07:39 +02:00
}
async function translate(language){
2019-11-30 02:05:37 +01:00
"use strict";
2018-09-12 00:07:39 +02:00
const originalData = await loadTranslation(language);
const data = {};
2019-11-30 02:05:37 +01:00
for (let i = 0; i < enKeys.length; i += 1){
2018-09-12 00:07:39 +02:00
const key = enKeys[i];
const oldData = originalData[key];
const enData = en[key];
if (oldData && oldData.message && oldData.message.trim()){
data[key] = oldData;
}
else {
data[key] = {
message: enData.message.trim() === ""? "": await askForTranslation(key),
description: (oldData && oldData.description) || enData.description
};
}
}
return data;
}
2019-12-28 23:23:55 +01:00
(async function(){
2019-11-30 02:05:37 +01:00
"use strict";
2019-12-28 23:23:55 +01:00
const data = await translate(language);
2019-11-30 02:05:37 +01:00
2019-12-28 23:23:55 +01:00
saveTranslation(language, data);
}());