UNPKG

react-intlayer

Version:

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

33 lines 1.06 kB
"use client"; import { getIntlayer, getIntlayerAsync } from "@intlayer/core"; import { useContext, useEffect, useMemo, useState } from "react"; import { IntlayerClientContext } from "./IntlayerProvider.mjs"; const useIntlayerAsync = (key, locale) => { const { locale: currentLocale } = useContext(IntlayerClientContext); const localeTarget = locale ?? currentLocale; const localeDictionary = getIntlayer(key, localeTarget); const [distantDictionary, setDistantDictionary] = useState(void 0); const [isLoading, setIsLoading] = useState(false); useEffect(() => { setIsLoading(true); getIntlayerAsync(key).then((distantDictionary2) => { if (distantDictionary2) { setDistantDictionary(distantDictionary2); } }).finally(() => { setIsLoading(false); }); }, []); const dictionary = useMemo( () => distantDictionary ?? localeDictionary, [distantDictionary, localeDictionary] ); return { ...dictionary, isLoading }; }; export { useIntlayerAsync }; //# sourceMappingURL=useIntlayerAsync.mjs.map