import { loadDynamicDictionary } from '@intlayer/core/dictionaryManipulator'; import { resolveInterpreterLocale } from '@intlayer/core/interpreter'; import type { Dictionary } from '@intlayer/types/dictionary'; import type { DeclaredLocales, LocalesValues, StrictModeLocaleMap, } from '@intlayer/types/module_augmentation'; import { IntlayerServerContext } from './IntlayerServerProvider'; import { getServerContext } from './serverContext'; import { useDictionary } from './useDictionary'; /** * On the server side, Hook that transform a dictionary and return the content * * If the locale is not provided, it will use the locale from the server context */ export const useDictionaryAsync = async < const T extends Dictionary, const L extends LocalesValues = DeclaredLocales, >( dictionaryPromise: StrictModeLocaleMap<() => Promise>, locale?: L, fallbackLocale?: DeclaredLocales ) => { const localeTarget = locale ?? getServerContext(IntlayerServerContext) ?? fallbackLocale ?? resolveInterpreterLocale(); const dictionary = (await loadDynamicDictionary( dictionaryPromise, localeTarget )) as T; return useDictionary(dictionary, localeTarget as L); };