@@ -59,21 +59,18 @@ function expandPlaceholderedGlob(_pathPattern: string, sourceLocale: string): st
5959 docUrl : "invalidPathPattern" ,
6060 } ) ;
6161 }
62- // Throw error if pathPattern contains "[locale]" several times
63- if ( pathPattern . split ( "[locale]" ) . length > 2 ) {
64- throw new CLIError ( {
65- message : `Invalid path pattern: ${ pathPattern } . Path pattern must contain at most one "[locale]" placeholder.` ,
66- docUrl : "invalidPathPattern" ,
67- } ) ;
68- }
62+
6963 // Break down path pattern into parts
7064 const pathPatternChunks = pathPattern . split ( path . sep ) ;
7165 // Find the index of the segment containing "[locale]"
72- const localeSegmentIndex = pathPatternChunks . findIndex ( ( segment ) => segment . includes ( "[locale]" ) ) ;
73- // Find the position of the "[locale]" placeholder within the segment
74- const localePlaceholderIndex = pathPatternChunks [ localeSegmentIndex ] ?. indexOf ( "[locale]" ) ?? - 1 ;
66+ const localeSegmentIndexes = pathPatternChunks . reduce ( ( indexes , segment , index ) => {
67+ if ( segment . includes ( "[locale]" ) ) {
68+ indexes . push ( index ) ;
69+ }
70+ return indexes ;
71+ } , [ ] as number [ ] ) ;
7572 // substitute [locale] in pathPattern with sourceLocale
76- const sourcePathPattern = pathPattern . replace ( / \[ l o c a l e \] / g, sourceLocale ) ;
73+ const sourcePathPattern = pathPattern . replaceAll ( / \[ l o c a l e \] / g, sourceLocale ) ;
7774 // get all files that match the sourcePathPattern
7875 const sourcePaths = glob
7976 . sync ( sourcePathPattern , { follow : true , withFileTypes : true } )
@@ -83,14 +80,18 @@ function expandPlaceholderedGlob(_pathPattern: string, sourceLocale: string): st
8380 // transform each source file path back to [locale] placeholder paths
8481 const placeholderedPaths = sourcePaths . map ( ( sourcePath ) => {
8582 const sourcePathChunks = sourcePath . split ( path . sep ) ;
86- if ( localeSegmentIndex >= 0 && localePlaceholderIndex >= 0 ) {
87- const placeholderedPathChunk = sourcePathChunks [ localeSegmentIndex ] ;
88- const placeholderedSegment =
89- placeholderedPathChunk . substring ( 0 , localePlaceholderIndex ) +
90- "[locale]" +
91- placeholderedPathChunk . substring ( localePlaceholderIndex + sourceLocale . length ) ;
92- sourcePathChunks [ localeSegmentIndex ] = placeholderedSegment ;
93- }
83+ localeSegmentIndexes . forEach ( ( localeSegmentIndex ) => {
84+ // Find the position of the "[locale]" placeholder within the segment
85+ const localePlaceholderIndex = pathPatternChunks [ localeSegmentIndex ] ?. indexOf ( "[locale]" ) ?? - 1 ;
86+ if ( localeSegmentIndex >= 0 && localePlaceholderIndex >= 0 ) {
87+ const placeholderedPathChunk = sourcePathChunks [ localeSegmentIndex ] ;
88+ const placeholderedSegment =
89+ placeholderedPathChunk . substring ( 0 , localePlaceholderIndex ) +
90+ "[locale]" +
91+ placeholderedPathChunk . substring ( localePlaceholderIndex + sourceLocale . length ) ;
92+ sourcePathChunks [ localeSegmentIndex ] = placeholderedSegment ;
93+ }
94+ } ) ;
9495 const placeholderedPath = sourcePathChunks . join ( path . sep ) ;
9596 return placeholderedPath ;
9697 } ) ;
0 commit comments