uptime-kuma/extra/update-language-files/index.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

// Need to use ES6 to read language files
2021-09-01 12:08:27 +03:00
import fs from "fs";
import util from "util";
2022-03-02 10:48:08 +03:00
import rmSync from "../fs-rmSync.js";
2021-09-01 12:08:27 +03:00
/**
* Copy across the required language files
* Creates a local directory (./languages) and copies the required files
* into it.
* @param {string} langCode Code of language to update. A file will be
* created with this code if one does not already exist
* @param {string} baseLang The second base language file to copy. This
* will be ignored if set to "en" as en.js is copied by default
2021-09-01 12:08:27 +03:00
*/
function copyFiles(langCode, baseLang) {
if (fs.existsSync("./languages")) {
rmSync("./languages", { recursive: true });
2021-09-01 12:08:27 +03:00
}
fs.mkdirSync("./languages");
if (!fs.existsSync(`../../src/languages/${langCode}.js`)) {
fs.closeSync(fs.openSync(`./languages/${langCode}.js`, "a"));
} else {
fs.copyFileSync(`../../src/languages/${langCode}.js`, `./languages/${langCode}.js`);
}
fs.copyFileSync("../../src/languages/en.js", "./languages/en.js");
if (baseLang !== "en") {
fs.copyFileSync(`../../src/languages/${baseLang}.js`, `./languages/${baseLang}.js`);
}
2021-10-12 10:10:32 +03:00
}
/**
* Update the specified language file
* @param {string} langCode Language code to update
* @param {string} baseLang Second language to copy keys from
*/
async function updateLanguage(langCode, baseLangCode) {
const en = (await import("./languages/en.js")).default;
const baseLang = (await import(`./languages/${baseLangCode}.js`)).default;
2021-09-01 12:08:27 +03:00
let file = langCode + ".js";
console.log("Processing " + file);
const lang = await import("./languages/" + file);
2021-09-01 12:08:27 +03:00
let obj;
2021-09-01 12:08:27 +03:00
if (lang.default) {
obj = lang.default;
} else {
console.log("Empty file");
obj = {
languageName: "<Your Language name in your language (not in English)>"
};
}
// En first
for (const key in en) {
2022-03-27 15:56:42 +03:00
if (! obj[key]) {
obj[key] = en[key];
2021-09-01 12:08:27 +03:00
}
}
2021-09-01 12:08:27 +03:00
if (baseLang !== en) {
2021-09-01 12:08:27 +03:00
// Base second
for (const key in baseLang) {
2022-03-27 15:56:42 +03:00
if (! obj[key]) {
2021-09-01 12:08:27 +03:00
obj[key] = key;
}
}
}
2021-09-01 12:08:27 +03:00
const code = "export default " + util.inspect(obj, {
depth: null,
});
2021-09-01 12:08:27 +03:00
fs.writeFileSync(`../../src/languages/${file}`, code);
2021-09-01 12:08:27 +03:00
}
// Get command line arguments
const baseLangCode = process.env.npm_config_baselang || "en";
const langCode = process.env.npm_config_language;
// We need the file to edit
if (langCode == null) {
throw new Error("Argument --language=<code> must be provided");
}
console.log("Base Lang: " + baseLangCode);
console.log("Updating: " + langCode);
copyFiles(langCode, baseLangCode);
await updateLanguage(langCode, baseLangCode);
2022-03-02 10:48:08 +03:00
rmSync("./languages", { recursive: true });
console.log("Done. Fixing formatting by ESLint...");