From e538f064838336dce4ed9cc56578c9dbf693a06b Mon Sep 17 00:00:00 2001 From: Donkie Date: Tue, 18 Mar 2025 20:32:12 +0100 Subject: [PATCH] Added script to find missing locales in i18n.ts file --- client/package.json | 3 +- client/scripts/check-i18n.js | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 client/scripts/check-i18n.js diff --git a/client/package.json b/client/package.json index 34f41e2..020c535 100644 --- a/client/package.json +++ b/client/package.json @@ -59,7 +59,8 @@ "build": "tsc && refine build", "build.zip": "cd ./dist && zip -r spoolman-client.zip ./ ./ && cd ..", "preview": "refine start", - "refine": "refine" + "refine": "refine", + "check-i18n": "node scripts/check-i18n.js" }, "browserslist": { "production": [ diff --git a/client/scripts/check-i18n.js b/client/scripts/check-i18n.js new file mode 100644 index 0000000..7da1cc9 --- /dev/null +++ b/client/scripts/check-i18n.js @@ -0,0 +1,56 @@ +#!/usr/bin/env node + +import { readdirSync, readFileSync, statSync } from "fs"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +const LOCALES_DIR = join(__dirname, "../public/locales"); +const I18N_FILE = join(__dirname, "../src/i18n.ts"); + +const minLocaleFileSize = 1024 * 10; // Minimum 10kB for a locale file to be considered +function getLocaleFolders() { + return readdirSync(LOCALES_DIR).filter((folder) => { + const folderPath = join(LOCALES_DIR, folder); + const commonFilePath = join(folderPath, "common.json"); + return ( + statSync(folderPath).isDirectory() && + statSync(commonFilePath).isFile() && + statSync(commonFilePath).size >= minLocaleFileSize + ); + }); +} + +function getDeclaredLanguages() { + const i18nContent = readFileSync(I18N_FILE, "utf8"); + const languageMatches = [...i18nContent.matchAll(/\["(.*?)"\]:/g)]; + return languageMatches.map((match) => match[1]); +} + +function main() { + const foundLocales = new Set(getLocaleFolders()); + const declaredLocales = new Set(getDeclaredLanguages()); + + const missingLocales = [...foundLocales].filter((locale) => !declaredLocales.has(locale)); + + if (missingLocales.length > 0) { + console.error("❌ The following locales are missing from src/i18n.ts:"); + missingLocales.forEach((locale) => console.error(` - ${locale}`)); + console.error("⚠️ Please add them to the `languages` object in i18n.ts."); + console.log("Template:"); + for (const locale of missingLocales) { + console.log(`["${locale}"]: { + name: "", + fullCode: "", + djs: () => import("dayjs/locale/${locale.toLowerCase()}"), +},`); + } + process.exit(1); + } + + console.log("✅ All locales are properly declared in i18n.ts."); + process.exit(0); +} + +main();