* 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
This commit is contained in:
+15
-1
@@ -1,7 +1,21 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export function findLang(pathname: string) {
|
||||
return Object.values(Lang).find(
|
||||
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)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user