next-intlayer
Version:
Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.
43 lines (40 loc) • 1.33 kB
JavaScript
'use client';
import { useLocale as useLocale$1 } from "react-intlayer";
import { getLocalizedUrl, getPathWithoutLocale } from "@intlayer/core";
import { usePathname, useRouter } from "next/navigation.js";
import { useCallback, useEffect, useMemo, useState } from "react";
//#region src/client/useLocale.ts
const usePathWithoutLocale = () => {
const pathname = usePathname();
const [fullPath, setFullPath] = useState(pathname);
useEffect(() => {
const search = typeof window !== "undefined" ? window.location.search : "";
setFullPath(search ? `${pathname}${search}` : pathname);
}, [pathname]);
return useMemo(() => getPathWithoutLocale(fullPath), [fullPath]);
};
const useLocale = ({ onChange } = {}) => {
const { replace, push } = useRouter();
const pathWithoutLocale = usePathWithoutLocale();
return {
...useLocale$1({ onLocaleChange: useCallback((locale) => {
if (!onChange) return;
if (typeof onChange === "function") {
onChange(locale);
return;
}
const pathWithLocale = getLocalizedUrl(pathWithoutLocale, locale);
if (onChange === "replace") replace(pathWithLocale);
if (onChange === "push") push(pathWithLocale);
}, [
replace,
push,
pathWithoutLocale,
onChange
]) }),
pathWithoutLocale
};
};
//#endregion
export { useLocale };
//# sourceMappingURL=useLocale.mjs.map