website/src/components/LanguagePicker.astro

33 lines
638 B
Plaintext
Raw Normal View History

2024-03-26 16:36:18 +01:00
---
2024-03-28 18:30:52 +01:00
import { locales, defaultLocale } from "astro-i18n-aut";
2024-03-26 16:36:18 +01:00
import { useTranslations } from "../i18n/utils";
function translatePath(lang: string) {
2024-03-27 01:51:42 +01:00
const split = Astro.url.pathname.split("/").filter((s) => s.length);
if (split[0] in locales) {
split.shift();
}
if (lang === defaultLocale) {
return `/${split.join("/")}`;
}
return `/${[lang, ...split].join("/")}`;
2024-03-26 16:36:18 +01:00
}
2024-04-03 18:07:54 +02:00
const t = useTranslations(Astro.url);
2024-03-26 16:36:18 +01:00
---
<ul>
{
Object.entries(locales).map(([lang, label]) => (
<li>
2024-03-28 18:30:52 +01:00
<a href={translatePath(lang)} data-astro-prefetch>
{t(label as "de")}
</a>
2024-03-26 16:36:18 +01:00
</li>
))
}
</ul>