chore: Misc for partner-sas * Add global-error to partner-sas * Add redirect to lang in partner-sas * Actually use language from param * Increase test timeouts and use parsed lang to fix tests * Remove need to import serverClient to setup trpc Approved-by: Hrishikesh Vaipurkar
39 lines
1005 B
TypeScript
39 lines
1005 B
TypeScript
import "server-only"
|
|
|
|
import { headers } from "next/headers"
|
|
import { cache } from "react"
|
|
|
|
import { Lang } from "@scandic-hotels/common/constants/language"
|
|
import { languageSchema } from "@scandic-hotels/common/utils/languages"
|
|
|
|
const getRef = cache(() => ({ current: undefined as Lang | undefined }))
|
|
|
|
/**
|
|
* Set the language for the current request
|
|
*
|
|
* It works kind of like React's context,
|
|
* but on the server side, per request.
|
|
*
|
|
* @param newLang
|
|
*/
|
|
export function setLang(newLang: Lang) {
|
|
const parseResult = languageSchema.safeParse(newLang)
|
|
const parsedLanguage = parseResult.success ? parseResult.data : Lang.en
|
|
|
|
getRef().current = parsedLanguage
|
|
|
|
return parsedLanguage
|
|
}
|
|
|
|
/**
|
|
* Get the global language set for the current request
|
|
*/
|
|
export async function getLang(): Promise<Lang> {
|
|
const contextLang = getRef().current
|
|
const headersList = await headers()
|
|
const headerLang = headersList.get("x-lang") as Lang
|
|
|
|
const l = contextLang || headerLang || Lang.en
|
|
return l
|
|
}
|