@@ -22,12 +22,24 @@ export async function handleAIBatchTranslation(
2222 const { provider, sourceLanguage, targetLanguage, translator } = config ;
2323 const results : TranslationResult [ ] = [ ] ;
2424 const totalBatches = Math . ceil ( subtitles . length / batchSize ) ;
25+ let processedSubtitles = 0 ;
26+
27+ logMessage (
28+ `开始AI批量翻译:总共 ${ subtitles . length } 条字幕,分为 ${ totalBatches } 个批次,每批次 ${ batchSize } 条` ,
29+ 'info' ,
30+ ) ;
2531
2632 for ( let i = 0 ; i < subtitles . length ; i += batchSize ) {
2733 const batch = subtitles . slice ( i , i + batchSize ) ;
2834 const currentBatchIndex = Math . floor ( i / batchSize ) + 1 ;
2935 let retryCount = 0 ;
3036 let batchSuccess = false ;
37+ let batchResults : TranslationResult [ ] = [ ] ;
38+
39+ logMessage (
40+ `处理批次 ${ currentBatchIndex } /${ totalBatches } ,包含 ${ batch . length } 条字幕` ,
41+ 'info' ,
42+ ) ;
3143
3244 while ( ! batchSuccess && retryCount <= maxRetries ) {
3345 try {
@@ -89,7 +101,7 @@ export async function handleAIBatchTranslation(
89101
90102 const parsedValues = Object . values ( parsedContent ) ;
91103
92- const batchResults = batch . map ( ( subtitle , index ) => ( {
104+ batchResults = batch . map ( ( subtitle , index ) => ( {
93105 id : subtitle . id ,
94106 startEndTime : subtitle . startEndTime ,
95107 sourceContent : subtitle . content . join ( '\n' ) ,
@@ -106,7 +118,13 @@ export async function handleAIBatchTranslation(
106118 }
107119
108120 results . push ( ...batchResults ) ;
121+ processedSubtitles += batch . length ;
109122 batchSuccess = true ;
123+
124+ logMessage (
125+ `批次 ${ currentBatchIndex } /${ totalBatches } 翻译成功,已处理 ${ processedSubtitles } /${ subtitles . length } 条字幕` ,
126+ 'info' ,
127+ ) ;
110128 } else {
111129 throw new Error (
112130 'Invalid response format: Failed to parse JSON structure' ,
@@ -136,7 +154,7 @@ export async function handleAIBatchTranslation(
136154 'error' ,
137155 ) ;
138156 // 如果全部重试都失败,则添加失败记录,并继续下一批
139- const failedResults = batch . map ( ( subtitle ) => ( {
157+ batchResults = batch . map ( ( subtitle ) => ( {
140158 id : subtitle . id ,
141159 startEndTime : subtitle . startEndTime ,
142160 sourceContent : subtitle . content . join ( '\n' ) ,
@@ -145,22 +163,41 @@ export async function handleAIBatchTranslation(
145163
146164 // 对失败的结果也进行处理和保存
147165 if ( onTranslationResult ) {
148- await onTranslationResult ( failedResults ) ;
166+ await onTranslationResult ( batchResults ) ;
149167 }
150168
151- results . push ( ...failedResults ) ;
169+ results . push ( ...batchResults ) ;
170+ processedSubtitles += batch . length ;
152171 batchSuccess = true ; // 标记为完成,继续下一批次
172+
173+ logMessage (
174+ `批次 ${ currentBatchIndex } /${ totalBatches } 已标记为失败完成,继续下一批次` ,
175+ 'warning' ,
176+ ) ;
153177 }
154178 }
155179 }
156180
157- // 更新翻译进度
158- const progress = Math . min ( ( ( i + batchSize ) / subtitles . length ) * 100 , 100 ) ;
181+ // 更新翻译进度 - 使用实际处理的字幕数量计算
182+ const progress = Math . min (
183+ ( processedSubtitles / subtitles . length ) * 100 ,
184+ 100 ,
185+ ) ;
159186 if ( onProgress ) {
160187 onProgress ( progress ) ;
161188 }
189+
190+ logMessage (
191+ `进度更新: ${ progress . toFixed ( 2 ) } % (${ processedSubtitles } /${ subtitles . length } )` ,
192+ 'info' ,
193+ ) ;
162194 }
163195
196+ logMessage (
197+ `AI批量翻译完成:共处理 ${ processedSubtitles } 条字幕,成功 ${ results . filter ( ( r ) => ! r . targetContent . startsWith ( '[翻译失败:' ) ) . length } 条` ,
198+ 'info' ,
199+ ) ;
200+
164201 return results ;
165202}
166203
0 commit comments