Make update-language-files command more useful (#2198)

* [empty commit] pull request for Fix language update script

* Avoid mass changes with update-language-files

This commit updates the update-language-files script to prevent mass
changes as seen on a number of recent PRs where the contributer has
ran the script and comitted the results.
The script has been updated to now require the --language argument to
specify which language file to update. This ensures that only that file
is updated instead of all files. If the provided language code does not
already exist, a new file with that code is created. This should make
it easier to add new languages as you only need to pass the language
code to the script.
The base lang code is now also passed as an optional argument to negate
the need for a seperate script entry in package.json.
The script has been restructures into a couple of functions to make it
easier to understand.
ESlint now only checks the changed file instead of
them all in order to improve performance.

Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>

* Updated translation docs for new command

Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>

* [update-language-files] Add cross-env-shell

Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>
Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
This commit is contained in:
Matthew Nickson 2022-10-08 08:01:47 +01:00 committed by GitHub
parent df2f536845
commit f67d7cdf3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 41 deletions

View File

@ -1,51 +1,45 @@
// Need to use ES6 to read language files // Need to use ES6 to read language files
import fs from "fs"; import fs from "fs";
import path from "path";
import util from "util"; import util from "util";
import rmSync from "../fs-rmSync.js"; import rmSync from "../fs-rmSync.js";
// https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js
/** /**
* Look ma, it's cp -R. * Copy across the required language files
* @param {string} src The path to the thing to copy. * Creates a local directory (./languages) and copies the required files
* @param {string} dest The path to the new copy. * 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) { function copyFiles(langCode, baseLang) {
let exists = fs.existsSync(src); if (fs.existsSync("./languages")) {
let stats = exists && fs.statSync(src); rmSync("./languages", { recursive: true });
let isDirectory = exists && stats.isDirectory(); }
fs.mkdirSync("./languages");
if (isDirectory) { if (!fs.existsSync(`../../src/languages/${langCode}.js`)) {
fs.mkdirSync(dest); fs.closeSync(fs.openSync(`./languages/${langCode}.js`, "a"));
fs.readdirSync(src).forEach(function (childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else { } 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; * Update the specified language file
const files = fs.readdirSync("./languages"); * @param {string} langCode Language code to update
console.log("Files:", files); * @param {string} baseLang Second language to copy keys from
*/
for (const file of files) { async function updateLanguage(langCode, baseLangCode) {
if (! file.endsWith(".js")) { const en = (await import("./languages/en.js")).default;
console.log("Skipping " + file); const baseLang = (await import(`./languages/${baseLangCode}.js`)).default;
continue;
}
let file = langCode + ".js";
console.log("Processing " + file); console.log("Processing " + file);
const lang = await import("./languages/" + file); const lang = await import("./languages/" + file);
@ -83,5 +77,20 @@ for (const file of files) {
fs.writeFileSync(`../../src/languages/${file}`, code); 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=<code> must be provided");
}
console.log("Base Lang: " + baseLangCode);
console.log("Updating: " + langCode);
copyFiles(langCode, baseLangCode);
await updateLanguage(langCode, baseLangCode);
rmSync("./languages", { recursive: true }); rmSync("./languages", { recursive: true });
console.log("Done. Fixing formatting by ESLint..."); console.log("Done. Fixing formatting by ESLint...");

View File

@ -51,8 +51,7 @@
"test-nodejs16": "docker build --progress plain -f test/ubuntu-nodejs16.dockerfile .", "test-nodejs16": "docker build --progress plain -f test/ubuntu-nodejs16.dockerfile .",
"simple-dns-server": "node extra/simple-dns-server.js", "simple-dns-server": "node extra/simple-dns-server.js",
"simple-mqtt-server": "node extra/simple-mqtt-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 && cross-env-shell eslint ../../src/languages/$npm_config_language.js --fix",
"update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix",
"ncu-patch": "npm-check-updates -u -t patch", "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-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", "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",

View File

@ -1,10 +1,13 @@
# How to translate # How to translate
1. Fork this repo. 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 2. Run `npm run update-language-files --language=<code>` where `<code>`
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. is a valid ISO language code:
4. Your language file should be filled in. You can translate now. http://www.lingoes.net/en/translator/langcode.htm. You can also use
5. Add it into `languageList` constant. this command to check if there are new strings to
6. Make a [pull request](https://github.com/louislam/uptime-kuma/pulls) when you have done. 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. 😏 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. 😏