11import PathGenerator from "./controllers/path.ts" ;
22import translate , { getFileErrors } from "./controllers/recurTranslate.ts" ;
33import fs from "fs" ;
4+ import util from "util" ;
45import path from "path" ;
56import { fileURLToPath } from "url" ;
67import { dirname } from "path" ;
78import OpenAI from "openai" ;
9+ import { permission } from "process" ;
810
911// Get the directory name of the current module
1012const __filename = fileURLToPath ( import . meta. url ) ;
1113const __dirname = dirname ( __filename ) ;
1214
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+
1321// Global variables for tracking translation state
1422// These need to be accessible by signal handlers
1523let xmlFiles : string [ ] = [ ] ;
@@ -52,7 +60,7 @@ async function saveSummaryLog() {
5260 failedDel . push ( file . id ) ;
5361 }
5462 } )
55- ) . then ( ( ) => console . log ( "successfully deleted all files" ) )
63+ ) . then ( ( ) => console . log ( "successfully deleted all files" ) ) ;
5664
5765 const timestamp = new Date ( ) . toISOString ( ) . replace ( / [: .] / g, "-" ) ;
5866 let summaryLog = `
@@ -189,11 +197,11 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
189197}
190198
191199// 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 > {
193201 // Generate the corresponding cn file path
194202 const cnFilePath = enFilePath . replace (
195203 path . sep + "en" + path . sep ,
196- path . sep + "cn" + path . sep
204+ path . sep + lang + path . sep
197205 ) ;
198206
199207 try {
@@ -212,9 +220,9 @@ async function needsTranslation(enFilePath: string): Promise<boolean> {
212220 }
213221}
214222
215- export default async function fancyName ( path : string ) {
223+ export default async function fancyName ( path : string , language : string ) {
216224 const fullPath = PathGenerator ( path ) ;
217- await translate ( "Chinese" , fullPath ) ;
225+ await translate ( language , fullPath ) ;
218226}
219227
220228// use "all" to translate every xml
@@ -223,6 +231,20 @@ export default async function fancyName(path: string) {
223231 await setupCleanupHandlers ( ) ;
224232
225233 try {
234+ if ( ( process . argv [ 2 ] , process . argv [ 3 ] ) ) {
235+ fancyName ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
236+ return ;
237+ }
238+
239+ let languages : string [ ] = [ ] ;
240+
241+ if ( process . argv [ 2 ] === "all" ) {
242+ languages = await getDirectories ( path . join ( __dirname , "../xml" ) ) ;
243+ console . dir ( languages ) ;
244+ } else {
245+ languages . push ( process . argv [ 2 ] ) ;
246+ }
247+
226248 // Get the absolute path to the xml/en directory using proper path resolution
227249 const enDirPath = path . resolve ( __dirname , "../xml/en" ) ;
228250
@@ -234,19 +256,20 @@ export default async function fancyName(path: string) {
234256 }
235257
236258 console . log ( `Found ${ xmlFiles . length } XML files to check for translation` ) ;
237-
259+
260+ for ( const lang of languages ) {
238261 // Filter files that need translation
239262 filesToTranslate = [ ] ;
240263 for ( const file of xmlFiles ) {
241- if ( await needsTranslation ( file ) ) {
264+ if ( await needsTranslation ( file , lang ) ) {
242265 filesToTranslate . push ( file as never ) ;
243266 }
244267 }
245268
246269 console . log ( `${ filesToTranslate . length } files need translation` ) ;
247270
248271 if ( filesToTranslate . length === 0 ) {
249- console . log ( " No files need translation. Exiting." ) ;
272+ console . log ( ` No files need translation for ${ lang } .` ) ;
250273 return ;
251274 }
252275
@@ -267,7 +290,7 @@ export default async function fancyName(path: string) {
267290 batch . map ( async file => {
268291 try {
269292 console . log ( `Starting translation for ${ file } ` ) ;
270- await translate ( "Chinese" , file ) ;
293+ await translate ( lang , file ) ;
271294 return { file, success : true } ;
272295 } catch ( error ) {
273296 // Return failure with error but don't log yet
@@ -329,6 +352,7 @@ export default async function fancyName(path: string) {
329352
330353 // Save a detailed summary to a log file
331354 await saveSummaryLog ( ) ;
355+ }
332356 } catch ( e ) {
333357 console . error ( "Error during translation process:" , e ) ;
334358 }
0 commit comments