From 4ffb68717220efbeaef71456907e91255185f749 Mon Sep 17 00:00:00 2001 From: MiniDigger | Martin Date: Sat, 25 May 2024 12:42:00 +0200 Subject: [PATCH] fix: process locale files to remove empty strings, fixes fallback to en --- frontend/nuxt.config.ts | 26 ++-------------- frontend/src/i18n/i18n-util.ts | 45 ++++++++++++++++++++++++++++ frontend/src/i18n/locales/.gitignore | 4 +-- 3 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 frontend/src/i18n/i18n-util.ts diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.ts index 11a1e45da..954947738 100644 --- a/frontend/nuxt.config.ts +++ b/frontend/nuxt.config.ts @@ -1,35 +1,15 @@ -import * as fs from "node:fs"; import IconsResolver from "unplugin-icons/resolver"; // import EslintPlugin from "vite-plugin-eslint"; import Components from "unplugin-vue-components/vite"; import type { ProxyOptions } from "@nuxt-alt/proxy"; import { defineNuxtConfig } from "nuxt/config"; -import type { LocaleObject } from "@nuxtjs/i18n"; +import { loadLocales } from "./src/i18n/i18n-util"; const backendHost = process.env.BACKEND_HOST || "http://localhost:8080"; const local = true; // set to false if backendData should be fetched from staging. You might need to hard reload (Ctrl+F5) the next page you're on when changing this value const backendDataHost = process.env.BACKEND_DATA_HOST || (local ? "http://localhost:8080" : "https://hangar.papermc.dev"); const allowIndexing = process.env.HANGAR_ALLOW_INDEXING || "true"; -const locales: LocaleObject[] = []; -for (const file of fs.readdirSync("./src/i18n/locales")) { - if (file === "dum.json") { - locales.push({ - code: "dum", - file, - name: "In-Context Editor", - }); - } else if (file.endsWith(".json")) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const locale = require("./src/i18n/locales/" + file.replace(".json", "")); - locales.push({ - code: locale.meta.code, - file, - name: locale.meta.name, - }); - } -} - // https://v3.nuxtjs.org/api/configuration/nuxt.config export default defineNuxtConfig({ telemetry: false, @@ -87,9 +67,9 @@ export default defineNuxtConfig({ vueI18n: "./src/i18n/i18n.config.ts", strategy: "no_prefix", lazy: true, - langDir: "./i18n/locales", + langDir: "./i18n/locales/processed", defaultLocale: "en", - locales, + locales: loadLocales(), detectBrowserLanguage: false, compilation: { jit: false, diff --git a/frontend/src/i18n/i18n-util.ts b/frontend/src/i18n/i18n-util.ts new file mode 100644 index 000000000..89a07f732 --- /dev/null +++ b/frontend/src/i18n/i18n-util.ts @@ -0,0 +1,45 @@ +import fs from "node:fs"; +import type { LocaleObject } from "@nuxtjs/i18n"; + +const localeFolder = "./src/i18n/locales/"; +const processedFolder = localeFolder + "processed/"; + +export function loadLocales() { + const locales: LocaleObject[] = []; + for (const file of fs.readdirSync(localeFolder)) { + if (!file.endsWith(".json")) continue; + // eslint-disable-next-line @typescript-eslint/no-var-requires + const locale = require("./locales/" + file.replace(".json", "")); + removeEmptyStrings(locale); + if (!fs.existsSync(processedFolder)) fs.mkdirSync(processedFolder, {}); + fs.writeFileSync(processedFolder + file, JSON.stringify(locale)); + if (file === "dum.json") { + locales.push({ + code: "dum", + file, + name: "In-Context Editor", + }); + } else { + if (!locale.meta.code || !locale.meta.name) { + console.log("Invalid language file " + file + ", skipping..."); + continue; + } + locales.push({ + code: locale.meta.code, + file, + name: locale.meta.name, + }); + } + } + return locales; +} + +function removeEmptyStrings(obj: Record) { + for (const [key, value] of Object.entries(obj)) { + if (value === "") { + delete obj[key]; + } else if (typeof value === "object" && value !== null) { + removeEmptyStrings(value as Record); + } + } +} diff --git a/frontend/src/i18n/locales/.gitignore b/frontend/src/i18n/locales/.gitignore index f3dce6c54..c48ae54d0 100644 --- a/frontend/src/i18n/locales/.gitignore +++ b/frontend/src/i18n/locales/.gitignore @@ -1,4 +1,4 @@ -*.ts *.json -!en.ts !en.json +processed +processed/en.json