@@ -30,6 +30,8 @@ import { Link } from '~/components/link';
30
30
import { usePathname , useRouter } from '~/i18n/routing' ;
31
31
import { useSearch } from '~/lib/search' ;
32
32
33
+ import { getLocalizedPathname } from './_actions/localized-pathname' ;
34
+
33
35
interface Link {
34
36
label : string ;
35
37
href : string ;
@@ -862,21 +864,32 @@ function SearchResults({
862
864
) ;
863
865
}
864
866
865
- const useSwitchLocale = ( ) => {
867
+ const useSwitchLocale = ( { activeLocale } : { activeLocale : Locale | undefined } ) => {
866
868
const pathname = usePathname ( ) ;
867
869
const router = useRouter ( ) ;
868
870
const params = useParams ( ) ;
869
871
const searchParams = useSearchParams ( ) ;
870
872
871
873
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
+
873
881
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
+ } ,
878
890
{ locale } ,
879
- ) ,
891
+ ) ;
892
+ } ,
880
893
[ pathname , params , router , searchParams ] ,
881
894
) ;
882
895
} ;
@@ -892,7 +905,7 @@ function LocaleSwitcher({
892
905
} ) {
893
906
const activeLocale = locales . find ( ( locale ) => locale . id === activeLocaleId ) ;
894
907
const [ isPending , startTransition ] = useTransition ( ) ;
895
- const switchLocale = useSwitchLocale ( ) ;
908
+ const switchLocale = useSwitchLocale ( { activeLocale } ) ;
896
909
897
910
return (
898
911
< div className = { className } >
0 commit comments