From 3dc720ff9cdd1002f737a43faaf428fb4cc18316 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Mon, 9 Feb 2026 13:26:27 -0600 Subject: [PATCH] fix: locale routing --- packages/web/src/i18n/locales.ts | 18 +++++++++++++++++- packages/web/src/middleware.ts | 10 +++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/web/src/i18n/locales.ts b/packages/web/src/i18n/locales.ts index 50cdaecb5..67e36dfe1 100644 --- a/packages/web/src/i18n/locales.ts +++ b/packages/web/src/i18n/locales.ts @@ -69,7 +69,7 @@ const starts = [ ["en", "root"], ] as const -export function matchLocale(input: string) { +function parse(input: string) { let decoded = "" try { decoded = decodeURIComponent(input) @@ -79,6 +79,22 @@ export function matchLocale(input: string) { const value = decoded.trim().toLowerCase() if (!value) return null + return value +} + +export function exactLocale(input: string) { + const value = parse(input) + if (!value) return null + if (value in localeAlias) { + return localeAlias[value as keyof typeof localeAlias] + } + + return null +} + +export function matchLocale(input: string) { + const value = parse(input) + if (!value) return null if (value.startsWith("zh")) { if (value.includes("hant") || value.includes("-tw") || value.includes("-hk") || value.includes("-mo")) { diff --git a/packages/web/src/middleware.ts b/packages/web/src/middleware.ts index 5b2d33666..97d085dfb 100644 --- a/packages/web/src/middleware.ts +++ b/packages/web/src/middleware.ts @@ -1,5 +1,5 @@ import { defineMiddleware } from "astro:middleware" -import { matchLocale } from "./i18n/locales" +import { exactLocale, matchLocale } from "./i18n/locales" function docsAlias(pathname: string) { const hit = /^\/docs\/([^/]+)(\/.*)?$/.exec(pathname) @@ -7,12 +7,12 @@ function docsAlias(pathname: string) { const value = hit[1] ?? "" const tail = hit[2] ?? "" - const locale = matchLocale(value) + const locale = exactLocale(value) if (!locale) return null - if (locale === "root") return `/docs${tail}` - if (value === locale) return null - return `/docs/${locale}${tail}` + const next = locale === "root" ? `/docs${tail}` : `/docs/${locale}${tail}` + if (next === pathname) return null + return next } function localeFromCookie(header: string | null) {