import { useNavigate } from '@tanstack/react-router'; import { getPrefix } from 'intlayer'; import { useLocale } from 'react-intlayer'; import { localizeTo, type StripLocalePrefix, withLocaleParam, } from '#/components/localized-link'; import type { FileRouteTypes } from '#/routeTree.gen'; type NavigateFn = ReturnType; type BaseNavigateOptions = Parameters[0]; type LocalizedTo = StripLocalePrefix; export type LocalizedNavigateOptions = Omit< BaseNavigateOptions, 'to' | 'params' > & { to: LocalizedTo; params?: Omit, 'locale'>; }; type LocalizedNavigate = ( options: LocalizedNavigateOptions ) => ReturnType; export const useLocalizedNavigate = () => { const navigate = useNavigate(); const { locale } = useLocale(); const localizedNavigate: LocalizedNavigate = ({ params, to, ...rest }) => navigate({ ...rest, params: withLocaleParam( params, getPrefix(locale).localePrefix ) as BaseNavigateOptions['params'], to: localizeTo(to) as BaseNavigateOptions['to'], }); return localizedNavigate; };