feat(LOY-156): Improve language handling and localization in profile page * feat(LOY-156): Improve language handling and localization in profile page * refactor(LOY-156): Improve language display using Intl.DisplayNames * feat(LOY-156): Enhance country display with localized country names * refactor(LOY-156): Move countries data to a dedicated constants file & more type safe country mapping * feat(LOY-156): Update isValidLang to use languageSchema + German translation for membership terms and conditions * chore(LOY-156): language handling in profile component Approved-by: Christian Andolf
22 lines
583 B
TypeScript
22 lines
583 B
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
|
|
export const languageSchema = z.preprocess(
|
|
(arg) => (typeof arg === "string" ? arg.toLowerCase() : arg),
|
|
z.nativeEnum(Lang)
|
|
)
|
|
|
|
export function isValidLang(lang?: string): lang is Lang {
|
|
const result = languageSchema.safeParse(lang)
|
|
return result.success
|
|
}
|
|
|
|
export function findLang(pathname: string): Lang | undefined {
|
|
const langFromPath = Object.values(Lang).find(
|
|
(l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
|
|
)
|
|
|
|
return isValidLang(langFromPath) ? langFromPath : undefined
|
|
}
|