fix: my pages menu not translated * fix: my pages menu not translated fix: able to pass lang to getIntl() Approved-by: Michael Zetterberg
37 lines
899 B
TypeScript
37 lines
899 B
TypeScript
import "server-only"
|
|
|
|
import { createIntl, createIntlCache } from "@formatjs/intl"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
|
|
import { getLang } from "@/i18n/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 || getLang()
|
|
|
|
if (!instances[lang]) {
|
|
const messages = await getMessages(lang)
|
|
instances[lang] = createIntl<React.ReactNode>(
|
|
{
|
|
defaultLocale: Lang.en,
|
|
locale: lang,
|
|
messages,
|
|
},
|
|
cache
|
|
)
|
|
}
|
|
|
|
// Exclamation mark can be removed once we update TS to 5.8.2+
|
|
return instances[lang]!
|
|
}
|