diff --git a/front/Dockerfile b/front/Dockerfile index 5db071c6..b3f2ef61 100644 --- a/front/Dockerfile +++ b/front/Dockerfile @@ -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 . . diff --git a/front/Dockerfile.dev b/front/Dockerfile.dev index 2bfbec5a..b32d1539 100644 --- a/front/Dockerfile.dev +++ b/front/Dockerfile.dev @@ -3,6 +3,7 @@ WORKDIR /app COPY package.json bun.lock . COPY scripts scripts +COPY public public RUN bun install --frozen-lockfile COPY . . diff --git a/front/biome.json b/front/biome.json index 8c2c8f47..ca911e9e 100644 --- a/front/biome.json +++ b/front/biome.json @@ -1,6 +1,9 @@ { "extends": "//", "files": { - "includes": ["src/**"] + "includes": [ + "src/**", + "scripts/**" + ] } } diff --git a/front/public/translations/pt_br.json b/front/public/translations/pt-BR.json similarity index 100% rename from front/public/translations/pt_br.json rename to front/public/translations/pt-BR.json diff --git a/front/scripts/postinstall.ts b/front/scripts/postinstall.ts index 2ec18601..0f7a2a0c 100644 --- a/front/scripts/postinstall.ts +++ b/front/scripts/postinstall.ts @@ -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(); diff --git a/front/src/providers/translations.compile.ts b/front/src/providers/translations.compile.ts index 55b100f6..1296173f 100644 --- a/front/src/providers/translations.compile.ts +++ b/front/src/providers/translations.compile.ts @@ -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", +]; \ No newline at end of file