Files
web/utils/languages.ts
Joakim Jäderberg 873183ec2f * move setLang() to a root layout
* fix: findLang only returns acceptable languages
* fix: fallback to use header x-lang if we haven't setLang yet
* fix: languageSchema, allow uppercase

Approved-by: Linus Flood
2025-02-19 09:06:37 +00:00

22 lines
509 B
TypeScript

import { z } from "zod"
import { Lang } from "@/constants/languages"
export function findLang(pathname: string) {
const langFromPath = Object.values(Lang).find(
(l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
)
const parsedLang = languageSchema.safeParse(langFromPath)
if (!parsedLang.success) {
return undefined
}
return parsedLang.data
}
export const languageSchema = z.preprocess(
(arg) => (typeof arg === "string" ? arg.toLowerCase() : arg),
z.nativeEnum(Lang)
)