chore: create a utils/languages file

This commit is contained in:
Chuma McPhoy
2024-07-04 15:19:17 +02:00
parent f89d17cd7f
commit bfcef5b832
12 changed files with 31 additions and 54 deletions

View File

@@ -64,43 +64,3 @@ export const languageSelect = [
{ label: "Norwegian", value: "No" },
{ label: "Swedish", value: "Sv" },
]
// -- Lang util functions --
export function findLang(pathname: string) {
return Object.values(Lang).find(
(l) => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
)
}
/**
* Helper function to convert langs in uppercase or capitalized format (e.g. the Hotel endpoint)
* to to Lang enum.
*/
export function fromUppercaseToLangEnum(lang: string): Lang | undefined {
const lowerCaseLang = lang.toLowerCase()
return Object.values(Lang).find((l) => l === lowerCaseLang)
}
/**
* Helper function to convert Lang enum to uppercase
* Needed for certtain API (e.g. the Hotel endpoint).
*/
export const toUppercaseLang = (lang: Lang): string => {
switch (lang) {
case Lang.en:
return "En"
case Lang.sv:
return "Sv"
case Lang.no:
return "No"
case Lang.fi:
return "Fi"
case Lang.da:
return "Da"
case Lang.de:
return "De"
default:
throw new Error("Invalid language")
}
}