Skip to content

Commit 43ba645

Browse files
committed
Make language detection from url more robust
1 parent 4f65308 commit 43ba645

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/i18n/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { de, enUS } from "date-fns/locale";
22
import { ui, defaultLang } from "./ui";
33

44
export function getLangFromUrl(url: URL) {
5-
const [, lang] = url.pathname.split("/");
6-
if (lang in ui) return lang as keyof typeof ui;
5+
const candidates = url.pathname.split("/").filter((a) => a.length === 2);
6+
7+
if (candidates.includes("de")) {
8+
return "de";
9+
}
10+
11+
if (candidates.includes("en")) {
12+
return "en";
13+
}
14+
715
return defaultLang;
816
}
917

src/layouts/Layout.astro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
import { getLangFromUrl, useTranslations } from "../i18n/utils";
2+
import { getLangFromUrl } from "../i18n/utils";
33
import Head from "../components/Head.astro";
44
import "@/styles/globals.css";
55
import Footer from "../components/Footer.astro";
@@ -8,7 +8,6 @@ import Navigation from "../components/Navigation.astro";
88
const { title } = Astro.props;
99
1010
const lang = getLangFromUrl(Astro.url);
11-
const t = useTranslations(lang);
1211
---
1312

1413
<script is:inline>

0 commit comments

Comments
 (0)