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
36 lines
856 B
TypeScript
36 lines
856 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>> {
|
|
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]
|
|
}
|