1
1
import PathGenerator from "./controllers/path.ts" ;
2
2
import translate , { getFileErrors } from "./controllers/recurTranslate.ts" ;
3
3
import fs from "fs" ;
4
+ import util from "util" ;
4
5
import path from "path" ;
5
6
import { fileURLToPath } from "url" ;
6
7
import { dirname } from "path" ;
7
8
import OpenAI from "openai" ;
9
+ import { permission } from "process" ;
8
10
9
11
// Get the directory name of the current module
10
12
const __filename = fileURLToPath ( import . meta. url ) ;
11
13
const __dirname = dirname ( __filename ) ;
12
14
15
+ const readdir = util . promisify ( fs . readdir ) ;
16
+ const getDirectories = async source =>
17
+ ( await readdir ( source , { withFileTypes : true } ) )
18
+ . filter ( dirent => dirent . isDirectory ( ) )
19
+ . map ( dirent => dirent . name ) ;
20
+
13
21
// Global variables for tracking translation state
14
22
// These need to be accessible by signal handlers
15
23
let xmlFiles : string [ ] = [ ] ;
@@ -52,7 +60,7 @@ async function saveSummaryLog() {
52
60
failedDel . push ( file . id ) ;
53
61
}
54
62
} )
55
- ) . then ( ( ) => console . log ( "successfully deleted all files" ) )
63
+ ) . then ( ( ) => console . log ( "successfully deleted all files" ) ) ;
56
64
57
65
const timestamp = new Date ( ) . toISOString ( ) . replace ( / [: .] / g, "-" ) ;
58
66
let summaryLog = `
@@ -189,11 +197,11 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
189
197
}
190
198
191
199
// Function to check if a file needs translation
192
- async function needsTranslation ( enFilePath : string ) : Promise < boolean > {
200
+ async function needsTranslation ( enFilePath : string , lang : string ) : Promise < boolean > {
193
201
// Generate the corresponding cn file path
194
202
const cnFilePath = enFilePath . replace (
195
203
path . sep + "en" + path . sep ,
196
- path . sep + "cn" + path . sep
204
+ path . sep + lang + path . sep
197
205
) ;
198
206
199
207
try {
@@ -212,15 +220,29 @@ async function needsTranslation(enFilePath: string): Promise<boolean> {
212
220
}
213
221
}
214
222
215
- export default async function fancyName ( path : string ) {
223
+ export default async function fancyName ( path : string , language : string ) {
216
224
const fullPath = PathGenerator ( path ) ;
217
- await translate ( "Chinese" , fullPath ) ;
225
+ await translate ( language , fullPath ) ;
218
226
}
219
227
220
228
( async ( ) => {
221
229
await setupCleanupHandlers ( ) ;
222
230
223
231
try {
232
+ if ( ( process . argv [ 2 ] , process . argv [ 3 ] ) ) {
233
+ fancyName ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
234
+ return ;
235
+ }
236
+
237
+ let languages : string [ ] = [ ] ;
238
+
239
+ if ( process . argv [ 2 ] === "all" ) {
240
+ languages = await getDirectories ( path . join ( __dirname , "../xml" ) ) ;
241
+ console . dir ( languages ) ;
242
+ } else {
243
+ languages . push ( process . argv [ 2 ] ) ;
244
+ }
245
+
224
246
// Get the absolute path to the xml/en directory using proper path resolution
225
247
const enDirPath = path . resolve ( __dirname , "../xml/en" ) ;
226
248
@@ -230,19 +252,20 @@ export default async function fancyName(path: string) {
230
252
xmlFiles = await findAllXmlFiles ( enDirPath ) ;
231
253
232
254
console . log ( `Found ${ xmlFiles . length } XML files to check for translation` ) ;
233
-
255
+
256
+ for ( const lang of languages ) {
234
257
// Filter files that need translation
235
258
filesToTranslate = [ ] ;
236
259
for ( const file of xmlFiles ) {
237
- if ( await needsTranslation ( file ) ) {
260
+ if ( await needsTranslation ( file , lang ) ) {
238
261
filesToTranslate . push ( file as never ) ;
239
262
}
240
263
}
241
264
242
265
console . log ( `${ filesToTranslate . length } files need translation` ) ;
243
266
244
267
if ( filesToTranslate . length === 0 ) {
245
- console . log ( " No files need translation. Exiting." ) ;
268
+ console . log ( ` No files need translation for ${ lang } .` ) ;
246
269
return ;
247
270
}
248
271
@@ -263,7 +286,7 @@ export default async function fancyName(path: string) {
263
286
batch . map ( async file => {
264
287
try {
265
288
console . log ( `Starting translation for ${ file } ` ) ;
266
- await translate ( "Chinese" , file ) ;
289
+ await translate ( lang , file ) ;
267
290
return { file, success : true } ;
268
291
} catch ( error ) {
269
292
// Return failure with error but don't log yet
@@ -325,6 +348,7 @@ export default async function fancyName(path: string) {
325
348
326
349
// Save a detailed summary to a log file
327
350
await saveSummaryLog ( ) ;
351
+ }
328
352
} catch ( e ) {
329
353
console . error ( "Error during translation process:" , e ) ;
330
354
}
0 commit comments