UNPKG

react-intlayer

Version:

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

90 lines (87 loc) 3.07 kB
'use client'; import { EditorProvider } from "../editor/EditorProvider.mjs"; import { localeInStorage, setLocaleInStorage } from "./useLocaleStorage.mjs"; import { internationalization } from "@intlayer/config/built"; import { createContext, useContext, useEffect, useState } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; import { setIntlayerIdentifier } from "@intlayer/config/client"; import { localeResolver } from "@intlayer/core/localization"; //#region src/client/IntlayerProvider.tsx /** * Context that stores the current locale on the client side. */ const IntlayerClientContext = createContext({ locale: localeInStorage ?? internationalization?.defaultLocale, setLocale: () => null, isCookieEnabled: true }); /** * Hook that provides the current Intlayer client context. * * @returns The current Intlayer context values. */ const useIntlayerContext = () => useContext(IntlayerClientContext) ?? {}; /** * Provider that stores the current locale on the client side. * * This component is focused on content delivery without the editor features. * * @param props - The provider props. * @returns The provider component. */ const IntlayerProviderContent = ({ locale: localeProp, defaultLocale: defaultLocaleProp, children, setLocale: setLocaleProp, disableEditor, isCookieEnabled }) => { const { locales: availableLocales, defaultLocale: defaultLocaleConfig } = internationalization ?? {}; const [currentLocale, setCurrentLocale] = useState(localeProp ?? localeInStorage ?? defaultLocaleProp ?? defaultLocaleConfig); useEffect(() => { if (localeProp && localeProp !== currentLocale) setCurrentLocale(localeProp); }, [localeProp]); useEffect(() => { setIntlayerIdentifier(); }, []); const setLocaleBase = (newLocale) => { if (currentLocale.toString() === newLocale.toString()) return; if (!availableLocales?.map(String).includes(newLocale)) { console.error(`Locale ${newLocale} is not available`); return; } setCurrentLocale(newLocale); setLocaleInStorage(newLocale, isCookieEnabled); }; const setLocale = setLocaleProp ?? setLocaleBase; const resolvedLocale = localeResolver(currentLocale); return /* @__PURE__ */ jsx(IntlayerClientContext.Provider, { value: { locale: resolvedLocale, setLocale, disableEditor }, children }); }; /** * Main provider for Intlayer in React applications. * * It includes the editor provider by default, allowing for live content editing * if configured. * * @param props - The provider props. * @returns The provider component with editor support. * * @example * ```tsx * import { IntlayerProvider } from 'react-intlayer'; * * const App = () => ( * <IntlayerProvider> * <MyComponent /> * </IntlayerProvider> * ); * ``` */ const IntlayerProvider = ({ children, ...props }) => /* @__PURE__ */ jsxs(IntlayerProviderContent, { ...props, children: [/* @__PURE__ */ jsx(EditorProvider, {}), children] }); //#endregion export { IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, useIntlayerContext }; //# sourceMappingURL=IntlayerProvider.mjs.map