Skip to content

Commit f721c7a

Browse files
committed
feat: lang switcher
1 parent 0130e40 commit f721c7a

File tree

5 files changed

+72
-70
lines changed

5 files changed

+72
-70
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: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
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>
13-
<div class="left">
14-
<a href="https://auxolotl.org" class="brand">
15-
<Image class="icon" src={aux} alt="auxolotl.org logo" />
16-
<span class="title">auxolotl.org</span>
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+
>
16+
<div class="flex items-center">
17+
<a href="https://auxolotl.org" class="flex items-center text-lg gap-4">
18+
<Image class="w-12 h-12" src={aux} alt="auxolotl.org logo" />
19+
<span>Auxolotl</span>
1720
</a>
1821
</div>
1922
<div class="right">
@@ -23,67 +26,36 @@ const translation = useTranslations(lang);
2326
<li><a href="https://auxolotl.org/contribute">Contribute</a></li>
2427
-->
2528

26-
<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
27-
<li><a href="https://forum.auxolotl.org">{translation("header.community")}</a></li>
28-
<li><a href="https://github.com/auxolotl">GitHub</a></li>
29-
</ul>
30-
</nav>
31-
</div>
29+
<nav>
30+
<ul class="flex gap-4 items-center h-full">
31+
<!--
32+
<li><a href="https://auxolotl.org/contribute">Contribute</a></li>
33+
-->
34+
35+
<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
36+
<li><a href="https://forum.auxolotl.org">{translation("header.community")}</a></li>
37+
<li><a href="https://github.com/auxolotl">GitHub</a></li>
38+
39+
<select data-lang-selector class="bg-transparent">
40+
{
41+
Object.keys(languages).map((lang) => (
42+
<option value={lang} class="text-black">
43+
{languages[lang as keyof typeof languages]}
44+
</option>
45+
))
46+
}
47+
</select>
48+
</ul>
49+
</nav>
3250
</header>
3351

34-
<style>
35-
header {
36-
position: sticky;
37-
display: flex;
38-
justify-content: space-between;
39-
top: 0;
40-
background: rgb(var(--background));
41-
padding: 0.5rem 0;
42-
z-index: 100;
43-
}
44-
45-
.left {
46-
display: flex;
47-
align-items: center;
48-
padding-left: 0.5rem;
49-
}
50-
51-
.left .brand {
52-
display: flex;
53-
align-items: center;
54-
font-size: 1.5rem;
55-
}
56-
57-
.left .brand .title {
58-
padding-left: 0.5rem;
59-
}
60-
61-
.icon {
62-
width: 48px;
63-
height: 48px;
64-
}
65-
66-
.right {
67-
display: flex;
68-
justify-content: flex-end;
69-
align-items: center;
70-
padding-right: 1rem;
71-
}
72-
73-
.right nav {
74-
display: flex;
75-
align-items: center;
76-
}
77-
78-
.right nav ul {
79-
display: flex;
80-
list-style: none;
81-
}
82-
83-
.right nav ul li {
84-
}
85-
86-
.right nav ul li:not(:last-child) {
87-
margin-right: 1rem;
88-
}
89-
</style>
52+
<script>
53+
import { switchLang } from "../i18n/utils";
54+
import type { languages } from "../i18n/ui";
55+
const select: HTMLSelectElement | null = document.querySelector(
56+
"[data-lang-selector]",
57+
);
58+
select?.addEventListener("change", () => {
59+
switchLang(select.value as keyof typeof languages);
60+
});
61+
</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 />

0 commit comments

Comments
 (0)