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 6, 2024
1 parent 7d1829b commit 1372d1d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 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"
}
}
42 changes: 35 additions & 7 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
---
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 class="sticky flex justify-between top-0 bg-[rgb(var(--background))] z-10 max-w-4xl mx-auto p-4 text-lg">
<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>
<span>Auxolotl</span>
</a>
</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.aux.computer">{translation("header.community")}</a></li>
<li>
<a href="https://forum.aux.computer"
>{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>

<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
13 changes: 8 additions & 5 deletions tailwind.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
theme: {
extend: {},
},
plugins: [require("@tailwindcss/typography")],
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
theme: {
extend: {},
},
plugins: [
require("@tailwindcss/typography"),
require("@tailwindcss/forms"),
],
};

0 comments on commit 1372d1d

Please sign in to comment.