0
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-11-28 10:36:39 +01:00
uptime-kuma/extra/update-language-files/index.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

// Need to use ES6 to read language files
2021-09-01 11:08:27 +02:00
import fs from "fs";
import util from "util";
/**
* 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
* @returns {void}
2021-09-01 11:08:27 +02:00
*/
function copyFiles(langCode, baseLang) {
if (fs.existsSync("./languages")) {
fs.rmSync("./languages", {
recursive: true,
force: true,
});
2021-09-01 11:08:27 +02: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 09:10:32 +02:00
}
/**
* Update the specified language file
* @param {string} langCode Language code to update
* @param {string} baseLangCode Second language to copy keys from
* @returns {void}
*/
async function updateLanguage(langCode, baseLangCode) {
const en = (await import("./languages/en.js")).default;
const baseLang = (await import(`./languages/${baseLangCode}.js`)).default;
2021-09-01 11:08:27 +02:00
let file = langCode + ".js";
console.log("Processing " + file);
const lang = await import("./languages/" + file);
2021-09-01 11:08:27 +02:00
let obj;
2021-09-01 11:08:27 +02: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 14:56:42 +02:00
if (! obj[key]) {
obj[key] = en[key];
2021-09-01 11:08:27 +02:00
}
}
2021-09-01 11:08:27 +02:00
if (baseLang !== en) {
2021-09-01 11:08:27 +02:00
// Base second
for (const key in baseLang) {
2022-03-27 14:56:42 +02:00
if (! obj[key]) {
2021-09-01 11:08:27 +02:00
obj[key] = key;
}
}
}
2021-09-01 11:08:27 +02:00
const code = "export default " + util.inspect(obj, {
depth: null,
});
2021-09-01 11:08:27 +02:00
fs.writeFileSync(`../../src/languages/${file}`, code);
2021-09-01 11:08:27 +02: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);
fs.rmSync("./languages", {
recursive: true,
force: true,
});
console.log("Done. Fixing formatting by ESLint...");