UNPKG

react-intlayer

Version:

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

38 lines (35 loc) 1.23 kB
'use client'; import { IntlayerClientContext } from "../client/IntlayerProvider.mjs"; import { useContext, useEffect, useRef } from "react"; import { isEnabled } from "@intlayer/editor/isEnabled"; //#region src/editor/useEditor.tsx /** * Initializes the Intlayer editor client singleton when the editor is enabled. * Syncs the current locale from the Intlayer context into the editor manager so * the editor always knows which locale the app is displaying. */ const useEditor = () => { const { locale } = useContext(IntlayerClientContext) ?? {}; const managerRef = useRef(null); useEffect(() => { if (process.env["INTLAYER_EDITOR_ENABLED"] === "false" || !isEnabled) return; import("@intlayer/editor").then(({ initEditorClient }) => { const manager = initEditorClient(); managerRef.current = manager; if (locale) manager.currentLocale.set(locale); }); return () => { managerRef.current = null; import("@intlayer/editor").then(({ stopEditorClient }) => { stopEditorClient(); }); }; }, []); useEffect(() => { if (!locale || !managerRef.current) return; managerRef.current.currentLocale.set(locale); }, [locale]); }; //#endregion export { useEditor }; //# sourceMappingURL=useEditor.mjs.map