Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface Props {}

import { useTranslations } from "../i18n/utils";
import type { Params } from "../i18n/utils";
import { languages } from "../i18n/ui";
import { Image } from "astro:assets";
import aux from "../../public/aux.svg";

Expand Down Expand Up @@ -33,6 +34,27 @@ const translation = useTranslations(lang);
>
</li>
<li><a href="https://github.com/auxolotl">GitHub</a></li>

<select data-lang-selector class="bg-transparent">
{
Object.keys(languages).map((lang) => (
<option value={lang} class="text-black">
{languages[lang as keyof typeof languages]}
</option>
))
}
</select>
</ul>
</nav>
</header>

<script>
import { switchLang } from "../i18n/utils";
import type { languages } from "../i18n/ui";
const select: HTMLSelectElement | null = document.querySelector(
"[data-lang-selector]",
);
select?.addEventListener("change", () => {
switchLang(select.value as keyof typeof languages);
});
</script>
6 changes: 6 additions & 0 deletions src/i18n/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export function useTranslations(lang: keyof typeof ui) {
};
}

export function switchLang(lang: keyof typeof ui) {
const parts = location.pathname.split("/");
parts[1] = lang;
location.href = parts.join("/");
}

export const getStaticPaths = (async () => {
return Object.keys(languages).map((name) => ({
params: { lang: name as keyof typeof languages },
Expand Down