Skip to content

Commit

Permalink
fix: process locale files to remove empty strings, fixes fallback to en
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed May 25, 2024
1 parent cb3844e commit 4ffb687
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
26 changes: 3 additions & 23 deletions frontend/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/i18n/i18n-util.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | object>) {
for (const [key, value] of Object.entries(obj)) {
if (value === "") {
delete obj[key];
} else if (typeof value === "object" && value !== null) {
removeEmptyStrings(value as Record<string, string | object>);
}
}
}
4 changes: 2 additions & 2 deletions frontend/src/i18n/locales/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.ts
*.json
!en.ts
!en.json
processed
processed/en.json

0 comments on commit 4ffb687

Please sign in to comment.