@intl-t/next
Version:
A Fully-Typed Object-Based i18n Translation Library for Next.js
30 lines (29 loc) • 1.06 kB
JavaScript
"use client";
import { useLocale } from "@intl-t/react";
import { resolvePath, resolveHref } from "@intl-t/tools/resolvers";
import { useRouter as ur, usePathname as up } from "next/navigation";
export { useLocale };
export const usePathname = () => resolvePath(up());
const state = {};
// @ts-ignore
function useResolvedRouter({ useRouter = ur, ...config } = this || {}) {
const router = useRouter();
let path = state.path?.();
let locale = state.locale?.();
const handler = (method) => ((href, { locale, ...options } = {}) => router[method](resolveHref(href, { ...config, locale }), options));
return {
...router,
push: handler("push"),
replace: handler("replace"),
prefetch: handler("prefetch"),
get pathname() {
state.path ||= usePathname;
return (path ||= state.path());
},
get locale() {
state.locale ||= () => useLocale()[0];
return (locale ||= state.locale());
},
};
}
export const useRouter = useResolvedRouter;