Skip to content

Commit

Permalink
feat: lang switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshmllow committed May 7, 2024
1 parent 0130e40 commit f721c7a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 70 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"license": "GPL-3.0-or-later",
"devDependencies": {
"@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.13"
}
}
112 changes: 42 additions & 70 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
---
interface Props {}
import { getLangFromUrl, useTranslations } from '../i18n/utils';
import { Image } from 'astro:assets';
import aux from '../../public/aux.svg';
import { getLangFromUrl, useTranslations } from "../i18n/utils";
import { Image } from "astro:assets";
import aux from "../../public/aux.svg";
import { languages } from "../i18n/ui";
const lang = getLangFromUrl(Astro.url);
const translation = useTranslations(lang);
---

<header>
<div class="left">
<a href="https://auxolotl.org" class="brand">
<Image class="icon" src={aux} alt="auxolotl.org logo" />
<span class="title">auxolotl.org</span>
<header
class="sticky flex justify-between top-0 bg-[rgb(var(--background))] z-10 max-w-4xl mx-auto p-4 text-lg"
>
<div class="flex items-center">
<a href="https://auxolotl.org" class="flex items-center text-lg gap-4">
<Image class="w-12 h-12" src={aux} alt="auxolotl.org logo" />
<span>Auxolotl</span>
</a>
</div>
<div class="right">
Expand All @@ -23,67 +26,36 @@ const translation = useTranslations(lang);
<li><a href="https://auxolotl.org/contribute">Contribute</a></li>
-->

<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
<li><a href="https://forum.auxolotl.org">{translation("header.community")}</a></li>
<li><a href="https://github.com/auxolotl">GitHub</a></li>
</ul>
</nav>
</div>
<nav>
<ul class="flex gap-4 items-center h-full">
<!--
<li><a href="https://auxolotl.org/contribute">Contribute</a></li>
-->

<li><a href="https://wiki.auxolotl.org">Wiki</a></li>
<li><a href="https://forum.auxolotl.org">{translation("header.community")}</a></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>

<style>
header {
position: sticky;
display: flex;
justify-content: space-between;
top: 0;
background: rgb(var(--background));
padding: 0.5rem 0;
z-index: 100;
}

.left {
display: flex;
align-items: center;
padding-left: 0.5rem;
}

.left .brand {
display: flex;
align-items: center;
font-size: 1.5rem;
}

.left .brand .title {
padding-left: 0.5rem;
}

.icon {
width: 48px;
height: 48px;
}

.right {
display: flex;
justify-content: flex-end;
align-items: center;
padding-right: 1rem;
}

.right nav {
display: flex;
align-items: center;
}

.right nav ul {
display: flex;
list-style: none;
}

.right nav ul li {
}

.right nav ul li:not(:last-child) {
margin-right: 1rem;
}
</style>
<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 @@ -6,6 +6,12 @@ export function getLangFromUrl(url: URL) {
return defaultLang;
}

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

export function useTranslations(lang: keyof typeof ui) {
return function t(key: keyof typeof ui[typeof defaultLang]) {
return ui[lang][key] || ui[defaultLang][key];
Expand Down
1 change: 1 addition & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const lang = getLangFromUrl(Astro.url);
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<meta name="verify" content="https://github.com/jakehamilton" />
<meta name="color-scheme" content="dark" />
</head>
<body>
<Header />
Expand Down

0 comments on commit f721c7a

Please sign in to comment.