@@ -34,28 +34,38 @@ function findLocaleFilesWithExtension(ext: string) {
3434 ignore : [ "node_modules/**" , "package*.json" , "i18n.json" , "lingo.json" ] ,
3535 } ) ;
3636
37- const localeFilePattern = new RegExp ( `[\/\\\\] ([a-z]{2}(-[A-Z]{2})?)${ ext } $` ) ;
38- const localeDirectoryPattern = new RegExp ( `[\/\\\\] ([a-z]{2}(-[A-Z]{2})?)[\/\\\\] [^\/\\\\ ]+${ ext } $` ) ;
37+ const localeFilePattern = new RegExp ( `\/ ([a-z]{2}(-[A-Z]{2})?)${ ext } $` ) ;
38+ const localeDirectoryPattern = new RegExp ( `\/ ([a-z]{2}(-[A-Z]{2})?)\/ [^\/]+${ ext } $` ) ;
3939 const potentialLocaleFiles = files . filter (
4040 ( file : string ) => localeFilePattern . test ( file ) || localeDirectoryPattern . test ( file ) ,
4141 ) ;
42- const localeFilesAndPatterns = potentialLocaleFiles
42+
43+ const potantialLocaleFilesAndPatterns = potentialLocaleFiles
4344 . map ( ( file : string ) => {
44- const match = file . match ( new RegExp ( `[\/|\\\\]([a-z]{2}(-[A-Z]{2})?)(\/|\\\\|${ ext } )` ) ) ;
45- const locale = match ?. [ 1 ] ;
46- const localeInDir = match ?. [ 3 ] !== ext ;
47- const filePattern = localeInDir
48- ? file . replace ( `/${ locale } /` , `/[locale]/` )
49- : path . join ( path . dirname ( file ) , `[locale]${ ext } ` ) ;
50- return { file, locale, pattern : filePattern } ;
45+ const matchPotentialLocales = file
46+ . matchAll ( new RegExp ( `\/([a-z]{2}(-[A-Z]{2})?|[^\/]+)(?=\/|${ ext } )` , "g" ) )
47+ . toArray ( ) ;
48+ const potantialLocales = matchPotentialLocales . map ( ( match ) => match [ 1 ] ) ;
49+ return { file, potantialLocales } ;
50+ } )
51+ . map ( ( { file, potantialLocales } ) => {
52+ for ( const locale of potantialLocales ) {
53+ try {
54+ resolveLocaleCode ( locale as LocaleCode ) ;
55+ return { locale, file } ;
56+ } catch ( e ) { }
57+ }
58+ return { file, locale : null } ;
5159 } )
52- . filter ( ( { locale } ) => {
53- try {
54- resolveLocaleCode ( locale as LocaleCode ) ;
55- return true ;
56- } catch ( e ) { }
57- return false ;
58- } ) ;
60+ . filter ( ( { locale } ) => locale !== null ) ;
61+
62+ const localeFilesAndPatterns = potantialLocaleFilesAndPatterns . map ( ( { file, locale } ) => {
63+ const localeInDir = file . match ( `/${ locale } /` ) ;
64+ const pattern = localeInDir
65+ ? file . replace ( `/${ locale } /` , `/[locale]/` )
66+ : path . join ( path . dirname ( file ) , `[locale]${ ext } ` ) ;
67+ return { pattern, file } ;
68+ } ) ;
5969
6070 const grouppedFilesAndPatterns = _ . groupBy ( localeFilesAndPatterns , "pattern" ) ;
6171 const patterns = Object . keys ( grouppedFilesAndPatterns ) ;
0 commit comments