-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathtranslate.ts
More file actions
43 lines (34 loc) · 1.09 KB
/
translate.ts
File metadata and controls
43 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import axios from "axios"
(<any>window)._translate = translate
let cache: any = {}
export async function translate(
testList: string[],
options: { server: string } = { server: "http://localhost:19212/prompt-studio/translate" }
) {
let resultList: string[][] = []
let reqList: [string, number][] = []
testList.forEach((text, i) => {
if (cache[text]) {
let t = cache[text]
resultList.push([text, t])
} else {
resultList.push([text])
reqList.push([text, i])
}
})
let rawText = reqList.map((req) => req[0]).join("\n")
let re = await axios.post(`${options.server}`, { text: rawText, to: "zh-cn" })
if (re && re.data) {
let list = re.data.split("\n")
list.forEach((text: string, i: number) => {
if (reqList[i]) {
let raw = reqList[i][0]
let index = reqList[i][1]
resultList[index].push(text)
cache[raw] = text
}
})
return resultList.map((x) => x[1])
}
}
translate.cache = cache