Skip to content

Commit 3fb6286

Browse files
committed
feat: lang switcher
1 parent 94eafb8 commit 3fb6286

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/components/Header.astro

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ interface Props {}
33
44
import { useTranslations } from "../i18n/utils";
55
import type { Params } from "../i18n/utils";
6+
import { languages } from "../i18n/ui";
67
import { Image } from "astro:assets";
78
import aux from "../../public/aux.svg";
89
@@ -33,6 +34,27 @@ const translation = useTranslations(lang);
3334
>
3435
</li>
3536
<li><a href="https://github.com/auxolotl">GitHub</a></li>
37+
38+
<select data-lang-selector class="bg-transparent">
39+
{
40+
Object.keys(languages).map((lang) => (
41+
<option value={lang} class="text-black">
42+
{languages[lang as keyof typeof languages]}
43+
</option>
44+
))
45+
}
46+
</select>
3647
</ul>
3748
</nav>
3849
</header>
50+
51+
<script>
52+
import { switchLang } from "../i18n/utils";
53+
import type { languages } from "../i18n/ui";
54+
const select: HTMLSelectElement | null = document.querySelector(
55+
"[data-lang-selector]",
56+
);
57+
select?.addEventListener("change", () => {
58+
switchLang(select.value as keyof typeof languages);
59+
});
60+
</script>

src/i18n/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export function useTranslations(lang: keyof typeof ui) {
1111
};
1212
}
1313

14+
export function switchLang(lang: keyof typeof ui) {
15+
const parts = location.pathname.split("/");
16+
parts[1] = lang;
17+
location.href = parts.join("/");
18+
}
19+
1420
export const getStaticPaths = (async () => {
1521
return Object.keys(languages).map((name) => ({
1622
params: { lang: name as keyof typeof languages },

0 commit comments

Comments
 (0)