Files
web/apps/partner-sas/i18n/index.ts
Joakim Jäderberg e4c5363fe9 Merged in fix/BOOK-399-default-language (pull request #2877)
fix(BOOK-399): Default language to 'en' when we've ended up in a invalid state

* fix(BOOK-399): Default language to 'en' when we've ended up in a invalid state

* PR fixes: reuse types and include local dev error message


Approved-by: Anton Gunnarsson
Approved-by: Linus Flood
2025-09-29 14:18:27 +00:00

40 lines
901 B
TypeScript

import "server-only"
import { createIntl, createIntlCache } from "@formatjs/intl"
import { Lang } from "@scandic-hotels/common/constants/language"
import { getLang } from "./serverContext"
import type { IntlShape } from "react-intl"
const cache = createIntlCache()
const instances: Partial<Record<Lang, IntlShape>> = {}
export async function getMessages(lang: Lang): Promise<Record<string, string>> {
if (!Lang[lang]) {
lang = Lang.en
}
return (await import(`./dictionaries/${lang}.json`)).default
}
export async function getIntl(options?: { lang: Lang | undefined }) {
const lang = options?.lang || (await getLang())
if (!instances[lang]) {
const messages = await getMessages(lang)
instances[lang] = createIntl<React.ReactNode>(
{
defaultLocale: Lang.en,
locale: lang,
messages,
},
cache
)
}
return instances[lang]
}