@@ -39,7 +39,7 @@ function curPageNodes(nodes: Node[], curPage: number) {
39
39
40
40
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
41
41
42
- async function traslate_all ( nodes : Node [ ] , lang : string , apiKey ?: string , notifyResult ?: any , useGoogle ?: boolean ) {
42
+ async function traslate_all ( nodes : Node [ ] , lang : string , apiKey ?: string , notifyResult ?: any , useGoogle ?: boolean , promptTemplate ?: string ) {
43
43
const batches : Node [ ] [ ] = [ ] ;
44
44
for ( let i = 0 ; i < nodes . length ; i += PAGE_SIZE ) {
45
45
batches . push ( nodes . slice ( i , i + PAGE_SIZE ) ) ;
@@ -51,7 +51,7 @@ async function traslate_all(nodes: Node[], lang: string, apiKey?: string, notify
51
51
let success = false ;
52
52
for ( let i = 0 ; i < MAX_RETRY && ! success ; i ++ ) {
53
53
try {
54
- const r = await translate_one_batch ( batch , lang , apiKey , useGoogle ) ;
54
+ const r = await translate_one_batch ( batch , lang , apiKey , useGoogle , promptTemplate ) ;
55
55
results . push ( ...r ) ;
56
56
success = true ;
57
57
if ( notifyResult ) {
@@ -72,7 +72,7 @@ async function traslate_all(nodes: Node[], lang: string, apiKey?: string, notify
72
72
return results ;
73
73
}
74
74
75
- async function translate_one_batch ( nodes : Node [ ] , lang : string , apiKey ?: string , useGoogle ?: boolean ) {
75
+ async function translate_one_batch ( nodes : Node [ ] , lang : string , apiKey ?: string , useGoogle ?: boolean , promptTemplate ?: string ) {
76
76
const sentences = nodes . map ( node => node . content ) ;
77
77
// if last sentence ends with ",", remove it
78
78
const lastSentence = sentences [ sentences . length - 1 ] ;
@@ -88,7 +88,8 @@ async function translate_one_batch(nodes: Node[], lang: string, apiKey?: string,
88
88
body : JSON . stringify ( {
89
89
"targetLang" : lang ,
90
90
"sentences" : sentences ,
91
- "apiKey" : apiKey
91
+ "apiKey" : apiKey ,
92
+ "promptTemplate" : promptTemplate
92
93
} )
93
94
} ;
94
95
@@ -157,6 +158,11 @@ export default function Srt() {
157
158
else return userLicenceKey ;
158
159
}
159
160
161
+ const getUserPrompt = ( ) => {
162
+ const res = localStorage . getItem ( "user-prompt-template" ) ;
163
+ if ( res ) return res ;
164
+ }
165
+
160
166
const getLang = ( ) => {
161
167
return ( document . getElementById ( "langSelect" ) as HTMLSelectElement ) . value ;
162
168
}
@@ -227,7 +233,7 @@ export default function Srt() {
227
233
const translateFile = async ( ) => {
228
234
setTransFileStatus ( { isTranslating : true , transCount : 0 } ) ;
229
235
try {
230
- const newnodes = await traslate_all ( nodes , getLang ( ) , getUserKey ( ) , on_trans_result , getUseGoogle ( ) ) ;
236
+ const newnodes = await traslate_all ( nodes , getLang ( ) , getUserKey ( ) , on_trans_result , getUseGoogle ( ) , getUserPrompt ( ) ) ;
231
237
//download("output.srt", nodesToSrtText(newnodes));
232
238
toast . success ( t ( "translate file successfully" ) ) ;
233
239
} catch ( e ) {
@@ -239,7 +245,7 @@ export default function Srt() {
239
245
const translate = async ( ) => {
240
246
setLoading ( true ) ;
241
247
try {
242
- const newnodes = await translate_one_batch ( curPageNodes ( nodes , curPage ) , getLang ( ) , getUserKey ( ) , getUseGoogle ( ) ) ;
248
+ const newnodes = await translate_one_batch ( curPageNodes ( nodes , curPage ) , getLang ( ) , getUserKey ( ) , getUseGoogle ( ) , getUserPrompt ( ) ) ;
243
249
setTransNodes ( nodes => {
244
250
const nodesCopy = [ ...nodes ] ;
245
251
for ( let i = 0 ; i < PAGE_SIZE ; i ++ ) {
0 commit comments