Skip to content

Commit ca43439

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

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

src/components/Header.astro

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
interface Props {}
33
44
import { getLangFromUrl, useTranslations } from '../i18n/utils';
5+
import { languages } from '../i18n/ui';
56
import { Image } from 'astro:assets';
67
import aux from '../../public/aux.svg';
78
@@ -26,64 +27,29 @@ const translation = useTranslations(lang);
2627
<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
2728
<li><a href="https://forum.auxolotl.org">{translation("header.community")}</a></li>
2829
<li><a href="https://github.com/auxolotl">GitHub</a></li>
30+
31+
<select data-lang-selector class="bg-transparent">
32+
{
33+
Object.keys(languages).map((lang) => (
34+
<option value={lang} class="text-black">
35+
{languages[lang as keyof typeof languages]}
36+
</option>
37+
))
38+
}
39+
</select>
2940
</ul>
3041
</nav>
3142
</div>
3243
</header>
3344

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-
}
45+
<script>
46+
import { switchLang } from "../i18n/utils";
47+
import type { languages } from "../i18n/ui";
48+
const select: HTMLSelectElement | null = document.querySelector(
49+
"[data-lang-selector]",
50+
);
51+
select?.addEventListener("change", () => {
52+
switchLang(select.value as keyof typeof languages);
53+
});
54+
</script>
8555

86-
.right nav ul li:not(:last-child) {
87-
margin-right: 1rem;
88-
}
89-
</style>

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)