UNPKG

react-intlayer

Version:

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

66 lines 2.25 kB
"use client"; import { jsx, jsxs } from "react/jsx-runtime"; import configuration from "@intlayer/config/built"; import { useCrossFrameState } from "@intlayer/editor-react"; import { createContext, useContext } from "react"; import { IntlayerEditorProvider } from "../editor/IntlayerEditorProvider.mjs"; import { PoweredByMeta } from "./PoweredByMeta.mjs"; import { localeCookie, setLocaleCookie } from "./useLocaleCookie.mjs"; import { localeResolver } from "@intlayer/core"; const IntlayerClientContext = createContext({ locale: localeCookie ?? configuration?.internationalization?.defaultLocale, setLocale: () => null, disableEditor: false }); const useIntlayerContext = () => useContext(IntlayerClientContext); const IntlayerProviderContent = ({ locale: localeProp, defaultLocale: defaultLocaleProp, children, setLocale: setLocaleProp, disableEditor }) => { const { internationalization } = configuration ?? {}; const { defaultLocale: defaultLocaleConfig, locales: availableLocales } = internationalization ?? {}; const defaultLocale = localeProp ?? localeCookie ?? defaultLocaleProp ?? defaultLocaleConfig; const [currentLocale, setCurrentLocale] = useCrossFrameState( "INTLAYER_CURRENT_LOCALE", defaultLocale ); 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); setLocaleCookie(newLocale); }; const setLocale = setLocaleProp ?? setLocaleBase; const resolvedLocale = localeResolver(localeProp ?? currentLocale); return /* @__PURE__ */ jsx( IntlayerClientContext.Provider, { value: { locale: resolvedLocale, setLocale, disableEditor }, children } ); }; const IntlayerProvider = (props) => /* @__PURE__ */ jsxs(IntlayerEditorProvider, { children: [ /* @__PURE__ */ jsx(PoweredByMeta, {}), /* @__PURE__ */ jsx(IntlayerProviderContent, { ...props }) ] }); export { IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, useIntlayerContext }; //# sourceMappingURL=IntlayerProvider.mjs.map