File tree Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ describe("swapLocale", () => {
1919 expect ( swapLocaleInUrl ( "/ro/get-started" , "ro" ) ) . toStrictEqual ( "/ro/get-started" ) ;
2020 expect ( swapLocaleInUrl ( "/en/get-started" , "ro" ) ) . toStrictEqual ( "/ro/get-started" ) ;
2121 expect ( swapLocaleInUrl ( "/ro/" , "en" ) ) . toStrictEqual ( "/en/" ) ;
22+ expect ( swapLocaleInUrl ( "/ro" , "en" ) ) . toStrictEqual ( "/en" ) ;
2223 } ) ;
2324} ) ;
2425
Original file line number Diff line number Diff line change @@ -35,7 +35,13 @@ export function mapLocale(locale: string) {
3535export function swapLocaleInUrl ( url : string , newLocale : string ) {
3636 const components = url . split ( "/" ) ;
3737 if ( components . length === 2 ) {
38- return `/${ newLocale } ${ url } ` ;
38+ const potentialLocale = components [ 1 ] ;
39+ const correspondingLocale = LOCALES . find ( l => l . id === potentialLocale ) ;
40+ if ( correspondingLocale ) {
41+ return `/${ newLocale } ` ;
42+ } else {
43+ return `/${ newLocale } ${ url } ` ;
44+ }
3945 } else {
4046 components [ 1 ] = newLocale ;
4147 return components . join ( "/" ) ;
You can’t perform that action at this time.
0 commit comments