Files
web/apps/partner-sas/i18n/serverContext.ts
Anton Gunnarsson 233c685e52 Merged in feat/sw-2333-package-and-sas-i18n (pull request #2538)
feat(SW-2333): I18n for multiple apps and packages

* Set upp i18n in partner-sas

* Adapt lokalise workflow to monorepo

* Fix layout props


Approved-by: Linus Flood
2025-07-10 07:00:03 +00:00

35 lines
939 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)
getRef().current = parseResult.success ? parseResult.data : Lang.en
}
/**
* 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
}