Skip to content

Commit 1372d1d

Browse files
committed
feat: lang switcher
1 parent 7d1829b commit 1372d1d

File tree

6 files changed

+73
-12
lines changed

6 files changed

+73
-12
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"license": "GPL-3.0-or-later",
2525
"devDependencies": {
26+
"@tailwindcss/forms": "^0.5.7",
2627
"@tailwindcss/typography": "^0.5.13"
2728
}
2829
}

src/components/Header.astro

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
---
22
interface Props {}
33
4-
import { getLangFromUrl, useTranslations } from '../i18n/utils';
5-
import { Image } from 'astro:assets';
6-
import aux from '../../public/aux.svg';
4+
import { getLangFromUrl, useTranslations } from "../i18n/utils";
5+
import { Image } from "astro:assets";
6+
import aux from "../../public/aux.svg";
7+
import { languages } from "../i18n/ui";
78
89
const lang = getLangFromUrl(Astro.url);
910
const translation = useTranslations(lang);
1011
---
1112

12-
<header class="sticky flex justify-between top-0 bg-[rgb(var(--background))] z-10 max-w-4xl mx-auto p-4 text-lg">
13+
<header
14+
class="sticky flex justify-between top-0 bg-[rgb(var(--background))] z-10 max-w-4xl mx-auto p-4 text-lg"
15+
>
1316
<div class="flex items-center">
1417
<a href="https://auxolotl.org" class="flex items-center text-lg gap-4">
1518
<Image class="w-12 h-12" src={aux} alt="auxolotl.org logo" />
16-
<span>Auxolotl<span>
19+
<span>Auxolotl</span>
1720
</a>
1821
</div>
1922

2023
<nav>
2124
<ul class="flex gap-4 items-center h-full">
22-
<!--
25+
<!--
2326
<li><a href="https://auxolotl.org/contribute">Contribute</a></li>
2427
-->
2528

2629
<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
27-
<li><a href="https://forum.aux.computer">{translation("header.community")}</a></li>
30+
<li>
31+
<a href="https://forum.aux.computer"
32+
>{translation("header.community")}</a
33+
>
34+
</li>
2835
<li><a href="https://github.com/auxolotl">GitHub</a></li>
36+
37+
<select data-lang-selector class="bg-transparent">
38+
{
39+
Object.keys(languages).map((lang) => (
40+
<option value={lang} class="text-black">
41+
{languages[lang as keyof typeof languages]}
42+
</option>
43+
))
44+
}
45+
</select>
2946
</ul>
3047
</nav>
3148
</header>
49+
50+
<script>
51+
import { switchLang } from "../i18n/utils";
52+
import type { languages } from "../i18n/ui";
53+
const select: HTMLSelectElement | null = document.querySelector(
54+
"[data-lang-selector]",
55+
);
56+
select?.addEventListener("change", () => {
57+
switchLang(select.value as keyof typeof languages);
58+
});
59+
</script>

src/i18n/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export function getLangFromUrl(url: URL) {
66
return defaultLang;
77
}
88

9+
export function switchLang(lang: keyof typeof ui) {
10+
const parts = location.pathname.split("/");
11+
parts[1] = lang;
12+
location.href = parts.join("/");
13+
}
14+
915
export function useTranslations(lang: keyof typeof ui) {
1016
return function t(key: keyof typeof ui[typeof defaultLang]) {
1117
return ui[lang][key] || ui[defaultLang][key];

src/layouts/Layout.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const lang = getLangFromUrl(Astro.url);
2121
<meta name="generator" content={Astro.generator} />
2222
<title>{title}</title>
2323
<meta name="verify" content="https://github.com/jakehamilton" />
24+
<meta name="color-scheme" content="dark" />
2425
</head>
2526
<body>
2627
<Header />

tailwind.config.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/** @type {import('tailwindcss').Config} */
22
export default {
3-
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
4-
theme: {
5-
extend: {},
6-
},
7-
plugins: [require("@tailwindcss/typography")],
3+
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
4+
theme: {
5+
extend: {},
6+
},
7+
plugins: [
8+
require("@tailwindcss/typography"),
9+
require("@tailwindcss/forms"),
10+
],
811
};

0 commit comments

Comments
 (0)