38 Zeilen
1.4 KiB
TypeScript
38 Zeilen
1.4 KiB
TypeScript
import type { AstroIntegration } from "astro";
|
|
import { mkdir, rename, access, constants } from 'node:fs/promises'
|
|
import {rm} from "fs/promises";
|
|
|
|
const locales = ["de"];
|
|
|
|
export default function configureI18n(): AstroIntegration {
|
|
return {
|
|
name: "astro-i18n-renamer",
|
|
hooks: {
|
|
"astro:build:done": async ({pages, dir, logger, routes}) => {
|
|
for (let page of pages) {
|
|
let [locale, ...rest] = page.pathname.split("/");
|
|
if (locales.includes(locale)) {
|
|
let path = rest.join("/");
|
|
let oldPath = `${dir.pathname}${page.pathname}`
|
|
let newPath = `${dir.pathname}${path}`
|
|
try {
|
|
await access(cutPrefix(newPath), constants.R_OK | constants.W_OK)
|
|
} catch (e) {
|
|
await mkdir(cutPrefix(newPath), {recursive: true});
|
|
}
|
|
|
|
await rename(`${cutPrefix(oldPath)}index.html`, `${cutPrefix(newPath)}index.${locale}.html`)
|
|
}
|
|
}
|
|
|
|
for (let locale of locales) {
|
|
await rm(`${cutPrefix(dir.pathname)}${locale}`, {recursive: true, force: true})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function cutPrefix(path: string): string {
|
|
return process.platform === "win32" ? path.substring(1) : path
|
|
} |