UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

1 lines 1.66 kB
{"version":3,"sources":["../../../src/client/useDictionaryAsync.ts"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\nimport type { Dictionary, LanguageContent } from '@intlayer/core';\nimport { useContext, useMemo } from 'react';\nimport { IntlayerClientContext } from './IntlayerProvider';\nimport { useDictionary } from './useDictionary';\n\n/**\n * On the server side, Hook that transform a dictionary and return the content\n *\n * If the locale is not provided, it will use the locale from the client context\n */\nexport const useDictionaryAsync = async <T extends Dictionary>(\n dictionaryPromise: LanguageContent<() => Promise<T>>,\n locale?: LocalesValues\n): Promise<T> => {\n const { locale: currentLocale } = useContext(IntlayerClientContext);\n\n const localeTarget = useMemo(\n () =>\n locale ??\n currentLocale ??\n configuration?.internationalization.defaultLocale,\n [currentLocale, locale]\n );\n\n const dictionary = await useMemo(\n async () => (await dictionaryPromise[localeTarget]!()) as T,\n [dictionaryPromise, localeTarget]\n );\n\n return useDictionary<T>(dictionary, localeTarget) as any;\n};\n"],"mappings":";AAEA,OAAO,mBAAmB;AAG1B,SAAS,YAAY,eAAe;AACpC,SAAS,6BAA6B;AACtC,SAAS,qBAAqB;AAOvB,MAAM,qBAAqB,OAChC,mBACA,WACe;AACf,QAAM,EAAE,QAAQ,cAAc,IAAI,WAAW,qBAAqB;AAElE,QAAM,eAAe;AAAA,IACnB,MACE,UACA,iBACA,eAAe,qBAAqB;AAAA,IACtC,CAAC,eAAe,MAAM;AAAA,EACxB;AAEA,QAAM,aAAa,MAAM;AAAA,IACvB,YAAa,MAAM,kBAAkB,YAAY,EAAG;AAAA,IACpD,CAAC,mBAAmB,YAAY;AAAA,EAClC;AAEA,SAAO,cAAiB,YAAY,YAAY;AAClD;","names":[]}