* 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
22 lines
509 B
TypeScript
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)
|
|
)
|