diff --git a/extra/update-language-files/index.js b/extra/update-language-files/index.js index e449fe347..078c4e6f8 100644 --- a/extra/update-language-files/index.js +++ b/extra/update-language-files/index.js @@ -1,51 +1,45 @@ // Need to use ES6 to read language files import fs from "fs"; -import path from "path"; import util from "util"; import rmSync from "../fs-rmSync.js"; -// https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js /** - * Look ma, it's cp -R. - * @param {string} src The path to the thing to copy. - * @param {string} dest The path to the new copy. + * 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 */ -const copyRecursiveSync = function (src, dest) { - let exists = fs.existsSync(src); - let stats = exists && fs.statSync(src); - let isDirectory = exists && stats.isDirectory(); +function copyFiles(langCode, baseLang) { + if (fs.existsSync("./languages")) { + rmSync("./languages", { recursive: true }); + } + fs.mkdirSync("./languages"); - if (isDirectory) { - fs.mkdirSync(dest); - fs.readdirSync(src).forEach(function (childItemName) { - copyRecursiveSync(path.join(src, childItemName), - path.join(dest, childItemName)); - }); + if (!fs.existsSync(`../../src/languages/${langCode}.js`)) { + fs.closeSync(fs.openSync(`./languages/${langCode}.js`, "a")); } else { - fs.copyFileSync(src, dest); + 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`); } -}; - -console.log("Arguments:", process.argv); -const baseLangCode = process.argv[2] || "en"; -console.log("Base Lang: " + baseLangCode); -if (fs.existsSync("./languages")) { - rmSync("./languages", { recursive: true }); } -copyRecursiveSync("../../src/languages", "./languages"); -const en = (await import("./languages/en.js")).default; -const baseLang = (await import(`./languages/${baseLangCode}.js`)).default; -const files = fs.readdirSync("./languages"); -console.log("Files:", files); - -for (const file of files) { - if (! file.endsWith(".js")) { - console.log("Skipping " + file); - continue; - } +/** + * 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; + let file = langCode + ".js"; console.log("Processing " + file); const lang = await import("./languages/" + file); @@ -83,5 +77,20 @@ for (const file of files) { fs.writeFileSync(`../../src/languages/${file}`, code); } +// 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= must be provided"); +} + +console.log("Base Lang: " + baseLangCode); +console.log("Updating: " + langCode); + +copyFiles(langCode, baseLangCode); +await updateLanguage(langCode, baseLangCode); rmSync("./languages", { recursive: true }); + console.log("Done. Fixing formatting by ESLint..."); diff --git a/package.json b/package.json index d478a1d24..f5f78f3ac 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,7 @@ "test-nodejs16": "docker build --progress plain -f test/ubuntu-nodejs16.dockerfile .", "simple-dns-server": "node extra/simple-dns-server.js", "simple-mqtt-server": "node extra/simple-mqtt-server.js", - "update-language-files-with-base-lang": "cd extra/update-language-files && node index.js %npm_config_base_lang% && eslint ../../src/languages/**.js --fix", - "update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix", + "update-language-files": "cd extra/update-language-files && node index.js && cross-env-shell eslint ../../src/languages/$npm_config_language.js --fix", "ncu-patch": "npm-check-updates -u -t patch", "release-final": "node extra/update-version.js && npm run build-docker && node ./extra/press-any-key.js && npm run upload-artifacts && node ./extra/update-wiki-version.js", "release-beta": "node extra/beta/update-version.js && npm run build && node ./extra/env2arg.js docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:$VERSION -t louislam/uptime-kuma:beta . --target release --push && node ./extra/press-any-key.js && npm run upload-artifacts", diff --git a/src/languages/README.md b/src/languages/README.md index d505476a8..eddd61022 100644 --- a/src/languages/README.md +++ b/src/languages/README.md @@ -1,10 +1,13 @@ # How to translate 1. Fork this repo. -2. Create a language file (e.g. `zh-TW.js`). The filename must be ISO language code: http://www.lingoes.net/en/translator/langcode.htm -3. Run `npm run update-language-files`. You can also use this command to check if there are new strings to translate for your language. -4. Your language file should be filled in. You can translate now. -5. Add it into `languageList` constant. -6. Make a [pull request](https://github.com/louislam/uptime-kuma/pulls) when you have done. +2. Run `npm run update-language-files --language=` where `` + is a valid ISO language code: + http://www.lingoes.net/en/translator/langcode.htm. You can also use + this command to check if there are new strings to + translate for your language. +3. Your language file should be filled in. You can translate now. +4. Add it into `languageList` constant. +5. Make a [pull request](https://github.com/louislam/uptime-kuma/pulls) when you have done. If you do not have programming skills, let me know in [the issues section](https://github.com/louislam/uptime-kuma/issues). I will assist you. 😏