@@ -34,9 +34,17 @@ Output:
3434 process . exit ( 0 )
3535}
3636
37- // Directory to traverse
38- const TARGET_DIR = path . join ( __dirname , "../webview-ui/src/components" )
39- const LOCALES_DIR = path . join ( __dirname , "../webview-ui/src/i18n/locales" )
37+ // Directories to traverse and their corresponding locales
38+ const DIRS = {
39+ components : {
40+ path : path . join ( __dirname , "../webview-ui/src/components" ) ,
41+ localesDir : path . join ( __dirname , "../webview-ui/src/i18n/locales" ) ,
42+ } ,
43+ src : {
44+ path : path . join ( __dirname , "../src" ) ,
45+ localesDir : path . join ( __dirname , "../src/i18n/locales" ) ,
46+ } ,
47+ }
4048
4149// Regular expressions to match i18n keys
4250const i18nPatterns = [
@@ -45,15 +53,23 @@ const i18nPatterns = [
4553 / t \( " ( [ a - z A - Z ] [ a - z A - Z 0 - 9 _ ] * [: .] [ a - z A - Z 0 - 9 _ . ] + ) " \) / g, // Match t("key") format, where key contains a colon or dot
4654]
4755
48- // Get all language directories
49- function getLocaleDirs ( ) {
50- const allLocales = fs . readdirSync ( LOCALES_DIR ) . filter ( ( file ) => {
51- const stats = fs . statSync ( path . join ( LOCALES_DIR , file ) )
52- return stats . isDirectory ( ) // Do not exclude any language directories
53- } )
56+ // Get all language directories for a specific locales directory
57+ function getLocaleDirs ( localesDir ) {
58+ try {
59+ const allLocales = fs . readdirSync ( localesDir ) . filter ( ( file ) => {
60+ const stats = fs . statSync ( path . join ( localesDir , file ) )
61+ return stats . isDirectory ( ) // Do not exclude any language directories
62+ } )
5463
55- // Filter to a specific language if specified
56- return args . locale ? allLocales . filter ( ( locale ) => locale === args . locale ) : allLocales
64+ // Filter to a specific language if specified
65+ return args . locale ? allLocales . filter ( ( locale ) => locale === args . locale ) : allLocales
66+ } catch ( error ) {
67+ if ( error . code === "ENOENT" ) {
68+ console . warn ( `Warning: Locales directory not found: ${ localesDir } ` )
69+ return [ ]
70+ }
71+ throw error
72+ }
5773}
5874
5975// Get the value from JSON by path
@@ -72,14 +88,14 @@ function getValueByPath(obj, path) {
7288}
7389
7490// Check if the key exists in all language files, return a list of missing language files
75- function checkKeyInLocales ( key , localeDirs ) {
91+ function checkKeyInLocales ( key , localeDirs , localesDir ) {
7692 const [ file , ...pathParts ] = key . split ( ":" )
7793 const jsonPath = pathParts . join ( "." )
7894
7995 const missingLocales = [ ]
8096
8197 localeDirs . forEach ( ( locale ) => {
82- const filePath = path . join ( LOCALES_DIR , locale , `${ file } .json` )
98+ const filePath = path . join ( localesDir , locale , `${ file } .json` )
8399 if ( ! fs . existsSync ( filePath ) ) {
84100 missingLocales . push ( `${ locale } /${ file } .json` )
85101 return
@@ -96,21 +112,20 @@ function checkKeyInLocales(key, localeDirs) {
96112
97113// Recursively traverse the directory
98114function findMissingI18nKeys ( ) {
99- const localeDirs = getLocaleDirs ( )
100115 const results = [ ]
101116
102- function walk ( dir ) {
117+ function walk ( dir , baseDir , localeDirs , localesDir ) {
103118 const files = fs . readdirSync ( dir )
104119
105120 for ( const file of files ) {
106121 const filePath = path . join ( dir , file )
107122 const stat = fs . statSync ( filePath )
108123
109- // Exclude test files
110- if ( filePath . includes ( ".test." ) ) continue
124+ // Exclude test files and __mocks__ directory
125+ if ( filePath . includes ( ".test." ) || filePath . includes ( "__mocks__" ) ) continue
111126
112127 if ( stat . isDirectory ( ) ) {
113- walk ( filePath ) // Recursively traverse subdirectories
128+ walk ( filePath , baseDir , localeDirs , localesDir ) // Recursively traverse subdirectories
114129 } else if ( stat . isFile ( ) && [ ".ts" , ".tsx" , ".js" , ".jsx" ] . includes ( path . extname ( filePath ) ) ) {
115130 const content = fs . readFileSync ( filePath , "utf8" )
116131
@@ -119,12 +134,12 @@ function findMissingI18nKeys() {
119134 let match
120135 while ( ( match = pattern . exec ( content ) ) !== null ) {
121136 const key = match [ 1 ]
122- const missingLocales = checkKeyInLocales ( key , localeDirs )
137+ const missingLocales = checkKeyInLocales ( key , localeDirs , localesDir )
123138 if ( missingLocales . length > 0 ) {
124139 results . push ( {
125140 key,
126141 missingLocales,
127- file : path . relative ( TARGET_DIR , filePath ) ,
142+ file : path . relative ( baseDir , filePath ) ,
128143 } )
129144 }
130145 }
@@ -133,21 +148,34 @@ function findMissingI18nKeys() {
133148 }
134149 }
135150
136- walk ( TARGET_DIR )
151+ // Walk through all directories
152+ Object . entries ( DIRS ) . forEach ( ( [ name , config ] ) => {
153+ const localeDirs = getLocaleDirs ( config . localesDir )
154+ if ( localeDirs . length > 0 ) {
155+ console . log ( `\nChecking ${ name } directory with ${ localeDirs . length } languages: ${ localeDirs . join ( ", " ) } ` )
156+ walk ( config . path , config . path , localeDirs , config . localesDir )
157+ }
158+ } )
159+
137160 return results
138161}
139162
140163// Execute and output the results
141164function main ( ) {
142165 try {
143- const localeDirs = getLocaleDirs ( )
144- if ( args . locale && localeDirs . length === 0 ) {
145- console . error ( `Error: Language '${ args . locale } ' not found in ${ LOCALES_DIR } ` )
146- process . exit ( 1 )
166+ if ( args . locale ) {
167+ // Check if the specified locale exists in any of the locales directories
168+ const localeExists = Object . values ( DIRS ) . some ( ( config ) => {
169+ const localeDirs = getLocaleDirs ( config . localesDir )
170+ return localeDirs . includes ( args . locale )
171+ } )
172+
173+ if ( ! localeExists ) {
174+ console . error ( `Error: Language '${ args . locale } ' not found in any locales directory` )
175+ process . exit ( 1 )
176+ }
147177 }
148178
149- console . log ( `Checking ${ localeDirs . length } non-English languages: ${ localeDirs . join ( ", " ) } ` )
150-
151179 const missingKeys = findMissingI18nKeys ( )
152180
153181 if ( missingKeys . length === 0 ) {
0 commit comments