Use a postinstall script to generate translation list

This commit is contained in:
Zoe Roux 2025-11-08 17:45:09 +01:00
parent 61708857af
commit 951ae955ed
No known key found for this signature in database
6 changed files with 125 additions and 28 deletions

View File

@ -5,8 +5,9 @@ WORKDIR /app
RUN apt update && apt install -y nodejs && rm /usr/local/bun-node-fallback-bin/node
ENV NODE_ENV=production
COPY package.json bun.lock scripts .
COPY package.json bun.lock .
COPY scripts scripts
COPY public public
RUN bun install --production
COPY . .

View File

@ -3,6 +3,7 @@ WORKDIR /app
COPY package.json bun.lock .
COPY scripts scripts
COPY public public
RUN bun install --frozen-lockfile
COPY . .

View File

@ -1,6 +1,9 @@
{
"extends": "//",
"files": {
"includes": ["src/**"]
"includes": [
"src/**",
"scripts/**"
]
}
}

View File

@ -1,12 +1,55 @@
import { readdir , mkdir } from 'node:fs/promises';
import { mkdir, readdir } from "node:fs/promises";
const srcDir = new URL("../node_modules/jassub/dist/", import.meta.url);
const destDir = new URL("../public/jassub/", import.meta.url);
async function jassub() {
const srcDir = new URL("../node_modules/jassub/dist/", import.meta.url);
const destDir = new URL("../public/jassub/", import.meta.url);
await mkdir(destDir, { recursive: true });
await mkdir(destDir, { recursive: true });
const files = await readdir(srcDir);
for (const file of files) {
const src = await Bun.file(new URL(file, srcDir)).arrayBuffer();
await Bun.write(new URL(file, destDir), src);
const files = await readdir(srcDir);
for (const file of files) {
const src = await Bun.file(new URL(file, srcDir)).arrayBuffer();
await Bun.write(new URL(file, destDir), src);
}
}
async function translations() {
const srcDir = new URL("../public/translations/", import.meta.url);
const dest = new URL(
"../src/providers/translations.compile.ts",
import.meta.url,
);
const translations = (await readdir(srcDir))
.map((x) => ({
file: x,
lang: x.replace(".json", ""),
var: x.replace(".json", "").replace("-", "_"),
}))
.map((x) => ({
...x,
quotedLang: x.lang.includes("-") ? `"${x.lang}"` : x.lang,
}))
.sort((a, b) => a.lang.localeCompare(b.lang));
await Bun.write(
dest,
`// this file is auto-generated via a postinstall script.
${translations
.map((x) => `import ${x.var} from "../../public/translations/${x.file}";`)
.join("\n")}
export const resources = {
${translations
.map((x) => `${x.quotedLang}: { translation: ${x.var} },`)
.join("\n\t")}
};
export const supportedLanguages = [
${translations.map((x) => `"${x.lang}",`).join("\n\t")}
];`,
);
}
await jassub();
await translations();

View File

@ -1,22 +1,71 @@
// this file is run at compile time thanks to a vite plugin
// import { readFile, readdir } from "node:fs/promises";
// import type { Resource } from "i18next";
// const translationDir = new URL("../../public/translations/", import.meta.url);
// const langs = await readdir(translationDir);
// export const resources: Resource = Object.fromEntries(
// await Promise.all(
// langs.map(async (x) => [
// x.replace(".json", ""),
// { translation: JSON.parse(await readFile(new URL(x, translationDir), "utf8")) },
// ]),
// ),
// );
// this file is auto-generated via a postinstall script.
import am from "../../public/translations/am.json";
import ar from "../../public/translations/ar.json";
import de from "../../public/translations/de.json";
import en from "../../public/translations/en.json";
import es from "../../public/translations/es.json";
import fr from "../../public/translations/fr.json";
import gl from "../../public/translations/gl.json";
import is from "../../public/translations/is.json";
import it from "../../public/translations/it.json";
import ko from "../../public/translations/ko.json";
import ml from "../../public/translations/ml.json";
import nl from "../../public/translations/nl.json";
import pl from "../../public/translations/pl.json";
import pt from "../../public/translations/pt.json";
import pt_BR from "../../public/translations/pt-BR.json";
import ro from "../../public/translations/ro.json";
import ru from "../../public/translations/ru.json";
import ta from "../../public/translations/ta.json";
import tr from "../../public/translations/tr.json";
import uk from "../../public/translations/uk.json";
import zh from "../../public/translations/zh.json";
export const resources = { en };
export const resources = {
am: { translation: am },
ar: { translation: ar },
de: { translation: de },
en: { translation: en },
es: { translation: es },
fr: { translation: fr },
gl: { translation: gl },
is: { translation: is },
it: { translation: it },
ko: { translation: ko },
ml: { translation: ml },
nl: { translation: nl },
pl: { translation: pl },
pt: { translation: pt },
"pt-BR": { translation: pt_BR },
ro: { translation: ro },
ru: { translation: ru },
ta: { translation: ta },
tr: { translation: tr },
uk: { translation: uk },
zh: { translation: zh },
};
export const supportedLanguages = Object.keys(resources);
export const supportedLanguages = [
"am",
"ar",
"de",
"en",
"es",
"fr",
"gl",
"is",
"it",
"ko",
"ml",
"nl",
"pl",
"pt",
"pt-BR",
"ro",
"ru",
"ta",
"tr",
"uk",
"zh",
];