Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
"astro": "astro",
"test": "bun test"
},
"dependencies": {
"@astrojs/cloudflare": "12.6.3",
Expand Down
34 changes: 34 additions & 0 deletions packages/web/src/i18n/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,37 @@ export function matchLocale(input: string) {

return starts.find((item) => value.startsWith(item[0]))?.[1] ?? null
}

export function parseAcceptLanguage(header: string | null): { lang: string; q: number }[] {
if (!header) return []

return header
.split(",")
.map((raw) => raw.trim())
.filter(Boolean)
.map((raw) => {
const parts = raw.split(";").map((x) => x.trim())
const lang = parts[0] ?? ""
const q = parts
.slice(1)
.find((x) => x.startsWith("q="))
?.slice(2)
return {
lang,
q: q ? Number.parseFloat(q) : 1,
}
})
.sort((a, b) => b.q - a.q)
}

export function localeFromAcceptLanguage(header: string | null) {
const items = parseAcceptLanguage(header)

const locale = items
.map((item) => item.lang)
.filter((lang) => lang && lang !== "*")
.map((lang) => matchLocale(lang))
.find((lang) => lang)

return locale ?? "root"
}
40 changes: 40 additions & 0 deletions packages/web/src/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, test } from "bun:test"
import { parseAcceptLanguage, localeFromAcceptLanguage } from "./i18n/locales"

describe("parseAcceptLanguage", () => {
test("returns empty array for null header", () => {
expect(parseAcceptLanguage(null)).toEqual([])
})

test("parses a single language with no q value as q=1", () => {
expect(parseAcceptLanguage("fr")).toEqual([{ lang: "fr", q: 1 }])
})

test("parses multiple languages and sorts by q value descending", () => {
const result = parseAcceptLanguage("en;q=0.5,fr;q=0.9,de")
expect(result.map((r) => r.lang)).toEqual(["de", "fr", "en"])
})

test("ignores empty segments from trailing commas", () => {
const result = parseAcceptLanguage("en,,fr")
expect(result.map((r) => r.lang)).toEqual(["en", "fr"])
})
})

describe("localeFromAcceptLanguage", () => {
test("returns 'root' for null header", () => {
expect(localeFromAcceptLanguage(null)).toBe("root")
})

test("picks the highest-weighted matching locale", () => {
expect(localeFromAcceptLanguage("en;q=0.5,fr;q=0.9")).toBe("fr")
})

test("returns 'root' when no listed language matches a known locale", () => {
expect(localeFromAcceptLanguage("xx-XX")).toBe("root")
})

test("skips wildcard entries", () => {
expect(localeFromAcceptLanguage("*;q=0.9,de;q=0.5")).toBe("de")
})
})
32 changes: 2 additions & 30 deletions packages/web/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineMiddleware } from "astro:middleware"
import { exactLocale, matchLocale } from "./i18n/locales"
import { exactLocale, matchLocale, localeFromAcceptLanguage } from "./i18n/locales"


function docsAlias(pathname: string) {
const hit = /^\/docs\/([^/]+)(\/.*)?$/.exec(pathname)
Expand Down Expand Up @@ -47,35 +48,6 @@ function localeFromCookie(header: string | null) {
return matchLocale(raw)
}

function localeFromAcceptLanguage(header: string | null) {
if (!header) return "root"

const items = header
.split(",")
.map((raw) => raw.trim())
.filter(Boolean)
.map((raw) => {
const parts = raw.split(";").map((x) => x.trim())
const lang = parts[0] ?? ""
const q = parts
.slice(1)
.find((x) => x.startsWith("q="))
?.slice(2)
return {
lang,
q: q ? Number.parseFloat(q) : 1,
}
})
.sort((a, b) => b.q - a.q)

const locale = items
.map((item) => item.lang)
.filter((lang) => lang && lang !== "*")
.map((lang) => matchLocale(lang))
.find((lang) => lang)

return locale ?? "root"
}

export const onRequest = defineMiddleware((ctx, next) => {
const alias = docsAlias(ctx.url.pathname)
Expand Down
Loading