2023-11-03 20:31:27 +01:00
|
|
|
import type { AstroIntegration } from "astro";
|
2024-01-06 21:36:22 +01:00
|
|
|
import { mkdir, access, constants, copyFile, rename } from "node:fs/promises";
|
2023-11-03 20:31:27 +01:00
|
|
|
|
2024-03-09 13:53:43 +01:00
|
|
|
const locales = ["en"];
|
2023-11-03 20:31:27 +01:00
|
|
|
|
|
|
|
export default function configureI18n(): AstroIntegration {
|
|
|
|
return {
|
|
|
|
name: "astro-i18n-renamer",
|
|
|
|
hooks: {
|
|
|
|
"astro:build:done": async ({pages, dir, logger, routes}) => {
|
2024-01-06 21:36:22 +01:00
|
|
|
for (const page of pages) {
|
|
|
|
const [locale, ...rest] = page.pathname.split("/");
|
2023-11-03 20:31:27 +01:00
|
|
|
if (locales.includes(locale)) {
|
2024-01-06 21:36:22 +01:00
|
|
|
const path = rest.join("/");
|
|
|
|
const oldPath = `${dir.pathname}${page.pathname}`;
|
|
|
|
const newPath = `${dir.pathname}${path}`;
|
2023-11-03 20:31:27 +01:00
|
|
|
try {
|
2024-01-06 21:36:22 +01:00
|
|
|
await access(cutPrefix(newPath), constants.R_OK | constants.W_OK);
|
2023-11-03 20:31:27 +01:00
|
|
|
} catch (e) {
|
|
|
|
await mkdir(cutPrefix(newPath), {recursive: true});
|
|
|
|
}
|
|
|
|
|
2024-01-06 21:36:22 +01:00
|
|
|
await copyFile(`${cutPrefix(oldPath)}index.html`, `${cutPrefix(newPath)}index.html.${locale}`);
|
|
|
|
logger.info(`Copied ${oldPath}index.html to ${newPath}index.html.${locale}`);
|
2023-12-15 16:11:33 +01:00
|
|
|
} else {
|
2024-01-06 21:36:22 +01:00
|
|
|
const oldPath = cutPrefix(`${dir.pathname}${page.pathname}`);
|
2024-03-09 13:53:43 +01:00
|
|
|
await rename(`${oldPath}index.html`, `${oldPath}index.html.de`);
|
2023-11-03 20:31:27 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-09 13:53:43 +01:00
|
|
|
},
|
|
|
|
},
|
2024-01-06 21:36:22 +01:00
|
|
|
};
|
2023-11-03 20:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function cutPrefix(path: string): string {
|
2024-01-06 21:36:22 +01:00
|
|
|
return process.platform === "win32" ? path.substring(1) : path;
|
2023-11-03 20:31:27 +01:00
|
|
|
}
|