112 lines
3.3 KiB
JavaScript
112 lines
3.3 KiB
JavaScript
import { mkdir, rm, writeFile, readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const pagesDir = path.join(root, "src", "pages");
|
|
const locale = "en";
|
|
const outputDir = path.join(pagesDir, locale);
|
|
|
|
const routeTranslations = new Map([
|
|
["jetzt-spielen", "join"],
|
|
["impressum", "imprint"],
|
|
["verhaltensrichtlinien", "code-of-conduct"],
|
|
["regeln", "rules"],
|
|
["rangliste", "ranked"],
|
|
["haeufige-fragen", "faq"],
|
|
["statistiken", "stats"],
|
|
["ankuendigungen", "announcements"],
|
|
["datenschutzerklaerung", "privacy-policy"],
|
|
["passwort-setzen", "set-password"],
|
|
]);
|
|
|
|
const pages = await collectAstroPages(pagesDir);
|
|
|
|
await rm(outputDir, { recursive: true, force: true });
|
|
|
|
for (const page of pages) {
|
|
const relativePage = path.relative(pagesDir, page);
|
|
const segments = relativePage.split(path.sep);
|
|
|
|
if (segments[0] === locale || segments[0] === "de") {
|
|
continue;
|
|
}
|
|
|
|
const outputSegments = translateSegments(segments);
|
|
const outFile = path.join(outputDir, ...outputSegments);
|
|
const source = await readFile(page, "utf8");
|
|
const hasGetStaticPaths = /\bexport\s+(?:const|async\s+function|function)\s+getStaticPaths\b/.test(source);
|
|
|
|
await mkdir(path.dirname(outFile), { recursive: true });
|
|
await writeFile(outFile, createProxyPage(page, outFile, hasGetStaticPaths));
|
|
}
|
|
|
|
function translateSegments(segments) {
|
|
const translated = [...segments];
|
|
|
|
if (translated[0] !== "index.astro" && !translated[0].startsWith("[")) {
|
|
translated[0] = routeTranslations.get(translated[0]) ?? translated[0];
|
|
}
|
|
|
|
const file = translated.pop();
|
|
const name = file.slice(0, -".astro".length);
|
|
|
|
if (name !== "index") {
|
|
translated.push(name);
|
|
}
|
|
|
|
translated.push("index.astro");
|
|
return translated;
|
|
}
|
|
|
|
function createProxyPage(sourceFile, outFile, hasGetStaticPaths) {
|
|
const outDir = path.dirname(outFile);
|
|
const importPath = normalizeImportPath(path.relative(outDir, sourceFile));
|
|
const lines = [
|
|
"---",
|
|
`import Page from "${importPath}";`,
|
|
];
|
|
|
|
if (hasGetStaticPaths) {
|
|
lines.push(
|
|
`import { getStaticPaths as proxyGetStaticPaths } from "${importPath}";`,
|
|
`export const getStaticPaths = (props) => proxyGetStaticPaths({ ...props, astroI18n: { locale: "${locale}" } });`,
|
|
);
|
|
}
|
|
|
|
lines.push(
|
|
"const { props } = Astro;",
|
|
"---",
|
|
"",
|
|
"<Page {...props} />",
|
|
"",
|
|
);
|
|
|
|
return lines.join("\n");
|
|
}
|
|
|
|
function normalizeImportPath(importPath) {
|
|
const normalized = importPath.split(path.sep).join("/");
|
|
return normalized.startsWith(".") ? normalized : `./${normalized}`;
|
|
}
|
|
|
|
async function collectAstroPages(dir) {
|
|
const entries = await import("node:fs/promises").then(({ readdir }) => readdir(dir, { withFileTypes: true }));
|
|
const files = [];
|
|
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
files.push(...await collectAstroPages(fullPath));
|
|
continue;
|
|
}
|
|
|
|
if (entry.isFile() && entry.name.endsWith(".astro")) {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|