@@ -8,7 +8,9 @@ const openai = new OpenAI({
88} ) ;
99
1010const SOURCE_DIR = 'docs' ;
11- const TARGET_LANGUAGES = [ 'de' , 'fr' , 'es' , 'ar' , 'pt' , 'th' , 'pl' , 'ja' ] ;
11+ const TARGET_LANGUAGES = [ 'de' , 'fr' , 'es' , 'ar' , 'pt' , 'th' , 'pl' , 'ja' , 'it' , 'sv' , 'nl' ] ;
12+
13+ const CONCURRENCY = 5 ;
1214
1315let totalPromptTokens = 0 ;
1416let totalCompletionTokens = 0 ;
@@ -64,9 +66,6 @@ function getAllMarkdownFiles() {
6466async function translateContent ( content , targetLang ) {
6567 const systemPrompt = buildSystemPrompt ( targetLang ) ;
6668
67- // console.log(`\n--- DEBUG: Prompt for ${targetLang} ---`);
68- // console.log(systemPrompt);
69-
7069 const response = await openai . chat . completions . create ( {
7170 model : 'gpt-4.1-mini' ,
7271 messages : [
@@ -91,17 +90,52 @@ async function translateContent(content, targetLang) {
9190 return response . choices [ 0 ] . message . content ;
9291}
9392
93+ async function runWithConcurrency ( tasks , worker , concurrency ) {
94+ let index = 0 ;
95+
96+ async function next ( ) {
97+ const current = index ++ ;
98+ if ( current >= tasks . length ) return ;
99+ const task = tasks [ current ] ;
100+ await worker ( task ) ;
101+ await next ( ) ;
102+ }
103+
104+ const workers = [ ] ;
105+ const workerCount = Math . min ( concurrency , tasks . length ) ;
106+ for ( let i = 0 ; i < workerCount ; i ++ ) {
107+ workers . push ( next ( ) ) ;
108+ }
109+ await Promise . all ( workers ) ;
110+ }
111+
94112async function main ( ) {
95113 const changedFiles = getChangedMarkdownFiles ( ) ;
96- console . log ( `Found ${ changedFiles . length } changed markdown files` ) ;
114+ console . log ( `Found ${ changedFiles . length } markdown files to translate` ) ;
115+
116+ const tasks = [ ] ;
97117
98118 for ( const file of changedFiles ) {
99119 const content = fs . readFileSync ( file , 'utf8' ) ;
100120 const relPath = path . relative ( SOURCE_DIR , file ) ;
101121
102122 for ( const lang of TARGET_LANGUAGES ) {
103- console . log ( `Translating ${ file } to ${ lang } ...` ) ;
123+ tasks . push ( {
124+ file,
125+ relPath,
126+ lang,
127+ content
128+ } ) ;
129+ }
130+ }
104131
132+ console . log ( `Created ${ tasks . length } translation tasks` ) ;
133+ console . log ( `Running with concurrency = ${ CONCURRENCY } ` ) ;
134+
135+ await runWithConcurrency (
136+ tasks ,
137+ async ( { file, relPath, lang, content } ) => {
138+ console . log ( `Translating ${ file } to ${ lang } ...` ) ;
105139 try {
106140 const translatedContent = await translateContent ( content , lang ) ;
107141
@@ -114,18 +148,17 @@ async function main() {
114148
115149 fs . mkdirSync ( path . dirname ( outputFile ) , { recursive : true } ) ;
116150 fs . writeFileSync ( outputFile , translatedContent ) ;
117-
118- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
119151 } catch ( error ) {
120152 console . error ( `Error translating ${ file } to ${ lang } :` , error ) ;
121153 }
122- }
123- }
154+ } ,
155+ CONCURRENCY
156+ ) ;
124157
125158 console . log ( '\n--- Translation Token Usage ---' ) ;
126159 console . log ( `Prompt tokens: ${ totalPromptTokens } ` ) ;
127160 console . log ( `Completion tokens: ${ totalCompletionTokens } ` ) ;
128161 console . log ( `Total tokens: ${ totalTokens } ` ) ;
129162}
130163
131- main ( ) . catch ( console . error ) ;
164+ main ( ) . catch ( console . error ) ;
0 commit comments