@@ -30,6 +30,8 @@ import { Link } from '~/components/link';
3030import { usePathname , useRouter } from '~/i18n/routing' ;
3131import { useSearch } from '~/lib/search' ;
3232
33+ import { getLocalizedPathname } from './_actions/localized-pathname' ;
34+
3335interface Link {
3436 label : string ;
3537 href : string ;
@@ -862,22 +864,33 @@ function SearchResults({
862864 ) ;
863865}
864866
865- const useSwitchLocale = ( ) => {
867+ const useSwitchLocale = ( { activeLocale } : { activeLocale : Locale | undefined } ) => {
866868 const pathname = usePathname ( ) ;
867869 const router = useRouter ( ) ;
868870 const params = useParams ( ) ;
869871 const searchParams = useSearchParams ( ) ;
870872
871873 return useCallback (
872- ( locale : string ) =>
874+ async ( locale : string ) => {
875+ const localizedPathname = await getLocalizedPathname ( {
876+ pathname,
877+ activeLocale : activeLocale ?. id ,
878+ targetLocale : locale ,
879+ } ) ;
880+
873881 router . push (
874- // @ts -expect-error -- TypeScript will validate that only known `params`
875- // are used in combination with a given `pathname`. Since the two will
876- // always match for the current route, we can skip runtime checks.
877- { pathname, params, query : Object . fromEntries ( searchParams . entries ( ) ) } ,
882+ {
883+ pathname : localizedPathname ,
884+ // @ts -expect-error -- TypeScript will validate that only known `params`
885+ // are used in combination with a given `pathname`. Since the two will
886+ // always match for the current route, we can skip runtime checks.
887+ params,
888+ query : Object . fromEntries ( searchParams . entries ( ) ) ,
889+ } ,
878890 { locale } ,
879- ) ,
880- [ pathname , params , router , searchParams ] ,
891+ ) ;
892+ } ,
893+ [ pathname , activeLocale ?. id , params , router , searchParams ] ,
881894 ) ;
882895} ;
883896
@@ -892,7 +905,7 @@ function LocaleSwitcher({
892905} ) {
893906 const activeLocale = locales . find ( ( locale ) => locale . id === activeLocaleId ) ;
894907 const [ isPending , startTransition ] = useTransition ( ) ;
895- const switchLocale = useSwitchLocale ( ) ;
908+ const switchLocale = useSwitchLocale ( { activeLocale } ) ;
896909
897910 return (
898911 < div className = { className } >
0 commit comments