UNPKG

next-intlayer

Version:

Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.

44 lines (41 loc) 1.5 kB
'use client'; const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs'); let react_intlayer = require("react-intlayer"); let __intlayer_core = require("@intlayer/core"); let next_navigation_js = require("next/navigation.js"); let react = require("react"); //#region src/client/useLocale.ts const usePathWithoutLocale = () => { const pathname = (0, next_navigation_js.usePathname)(); const [fullPath, setFullPath] = (0, react.useState)(pathname); (0, react.useEffect)(() => { const search = typeof window !== "undefined" ? window.location.search : ""; setFullPath(search ? `${pathname}${search}` : pathname); }, [pathname]); return (0, react.useMemo)(() => (0, __intlayer_core.getPathWithoutLocale)(fullPath), [fullPath]); }; const useLocale = ({ onChange } = {}) => { const { replace, push } = (0, next_navigation_js.useRouter)(); const pathWithoutLocale = usePathWithoutLocale(); return { ...(0, react_intlayer.useLocale)({ onLocaleChange: (0, react.useCallback)((locale) => { if (!onChange) return; if (typeof onChange === "function") { onChange(locale); return; } const pathWithLocale = (0, __intlayer_core.getLocalizedUrl)(pathWithoutLocale, locale); if (onChange === "replace") replace(pathWithLocale); if (onChange === "push") push(pathWithLocale); }, [ replace, push, pathWithoutLocale, onChange ]) }), pathWithoutLocale }; }; //#endregion exports.useLocale = useLocale; //# sourceMappingURL=useLocale.cjs.map