@@ -60,27 +60,18 @@ export interface PathLocale {
60
60
*/
61
61
export function normalizeLocalePath ( pathname : string , locales ?: string [ ] ) : PathLocale {
62
62
let detectedLocale : string | undefined
63
-
64
- // normalize the locales to lowercase
65
- const normalizedLocales = locales ?. map ( ( loc ) => loc . toLowerCase ( ) )
66
-
67
- // split the pathname into parts, removing the leading slash
68
- const pathnameParts = pathname . substring ( 1 ) . split ( '/' )
69
-
70
- // split the first part of the pathname to check if it's a locale
71
- const localeParts = pathnameParts [ 0 ] . toLowerCase ( ) . split ( '-' )
72
-
73
- // check if the first part of the pathname is a locale
74
- // by matching the given locales, with decreasing specificity
75
- for ( let i = localeParts . length ; i > 0 ; i -- ) {
76
- const localePart = localeParts . slice ( 0 , i ) . join ( '-' )
77
- if ( normalizedLocales ?. includes ( localePart ) ) {
78
- detectedLocale = localeParts . join ( '-' )
79
- pathname = `/${ pathnameParts . slice ( 1 ) . join ( '/' ) } `
80
- break
63
+ // first item will be empty string from splitting at first char
64
+ const pathnameParts = pathname . split ( '/' )
65
+
66
+ ; ( locales || [ ] ) . some ( ( locale ) => {
67
+ if ( pathnameParts [ 1 ] && pathnameParts [ 1 ] . toLowerCase ( ) === locale . toLowerCase ( ) ) {
68
+ detectedLocale = locale
69
+ pathnameParts . splice ( 1 , 1 )
70
+ pathname = pathnameParts . join ( '/' )
71
+ return true
81
72
}
82
- }
83
-
73
+ return false
74
+ } )
84
75
return {
85
76
pathname,
86
77
detectedLocale,
0 commit comments