Migrate Site to German as Default Locale

This commit is contained in:
2024-03-09 13:53:43 +01:00
parent fd56de0451
commit 9312089e96
26 changed files with 109 additions and 486 deletions

View File

@ -1,5 +1,5 @@
---
import {getCollection} from "astro:content";
import {CollectionEntry, getCollection} from "astro:content";
import {astroI18n, createGetStaticPaths} from "astro-i18n";
import PageLayout from "../layouts/PageLayout.astro";
import LanguageWarning from "../components/LanguageWarning.astro";
@ -8,31 +8,46 @@ import "@styles/table.css";
export const getStaticPaths = createGetStaticPaths(async () => {
let posts = await getCollection("pages", value => value.id.split("/")[0] === astroI18n.locale);
function fixLink(slug: string): string {
if (astroI18n.locales.includes(slug.split("/")[0])) {
return slug.split("/").slice(1).join("/");
const germanPosts = await getCollection("pages", entry => entry.id.split("/")[0] === astroI18n.fallbackLocale);
germanPosts.forEach(value => {
if (posts.find(post => post.id.split("/")[1] === value.id.split("/")[1])) {
return;
} else {
return slug;
posts.push(value);
}
});
function fixLink(page: CollectionEntry<"pages">): string {
if (astroI18n.locale != astroI18n.primaryLocale && Object.keys(page.data.slugs ?? {}).includes(astroI18n.locale)) {
return page.data.slugs[astroI18n.locale];
}
if (astroI18n.locales.includes(page.slug.split("/")[0])) {
return page.slug.split("/").slice(1).join("/");
} else {
return page.slug;
}
}
return posts.map((page) => ({
props: {
page,
german: page.id.split("/")[0] === astroI18n.fallbackLocale,
},
params: {
slug: fixLink(page.slug),
slug: fixLink(page),
},
}));
});
const {page} = Astro.props;
const {page, german} = Astro.props;
const {Content} = await page.render();
---
<PageLayout title={page.data.title}>
<article>
{page.data.german && (
{german && (
<LanguageWarning/>
)}
<h1 class="text-left">{page.data.title}</h1>