-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtranslate.ts
More file actions
223 lines (199 loc) · 6.03 KB
/
Copy pathtranslate.ts
File metadata and controls
223 lines (199 loc) · 6.03 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { createProxy } from 'node-fetch-native/proxy'
import { detectLang } from 'whatlang-node'
import { xfetch } from 'x-fetch'
import {
API_URL,
COMMON_HEADERS,
HTTP_STATUS_NOT_FOUND,
HTTP_STATUS_OK,
HTTP_STATUS_SERVICE_UNAVAILABLE,
type SourceLanguage,
type SupportedCode,
type TargetLanguage,
} from './constants.ts'
import type {
DeepLXTranslationResult,
Job,
PostData,
Translation,
TranslationResponse,
} from './types.ts'
import {
abbreviateLanguage,
formatPostString,
getICount,
getRandomNumber,
getTimeStamp,
} from './utils.ts'
// makeRequest makes an HTTP request to DeepL API
const makeRequest = async (
postData: PostData,
proxyUrl?: string,
dlSession?: string,
) => {
return xfetch<TranslationResponse>(API_URL, {
method: 'POST',
body: formatPostString(postData),
headers: dlSession
? { ...COMMON_HEADERS, Cookie: `dl_session=${dlSession}` }
: COMMON_HEADERS,
...createProxy({ url: proxyUrl }),
})
}
const splitAndProcess = (text: string): string[] =>
text.split('\n').map(line => (line.trim() === '' ? '\n' : line))
export const translateByDeepLX = async (
sourceLang: SourceLanguage | undefined,
targetLang: TargetLanguage,
text: string,
formal?: boolean,
tagHandling = 'plaintext',
proxyUrl?: string,
dlSession?: string,
// eslint-disable-next-line sonarjs/cognitive-complexity
): Promise<DeepLXTranslationResult> => {
if (!text) {
return { code: HTTP_STATUS_NOT_FOUND, message: 'No text to translate' }
}
// Split text by newlines and store them for later reconstruction
const textParts = splitAndProcess(text)
const translatedParts: string[] = []
const allAlternatives: string[][] = [] // Store alternatives for each part
for (const part of textParts) {
if (!part.trim()) {
translatedParts.push('')
allAlternatives.push([''])
continue
}
// Get detected language if source language is auto
if (!sourceLang || sourceLang === 'auto') {
sourceLang = detectLang(part, true) as SourceLanguage
}
const sourceLangCode =
abbreviateLanguage(sourceLang) ??
(sourceLang.toUpperCase() as SupportedCode)
// Prepare jobs from split result
const jobs: Job[] = [
{
kind: 'default',
preferred_num_beams: 4,
raw_en_context_before: [],
raw_en_context_after: [],
sentences: [{ prefix: '', text: part, id: 0 }],
},
]
let hasRegionalVariant = false
let targetLangCode =
abbreviateLanguage(targetLang) ??
(targetLang.toUpperCase() as SupportedCode)
const targetLangParts = targetLang.split('-')
if (targetLangParts.length > 1) {
targetLangCode = targetLangParts[0] as SupportedCode
hasRegionalVariant = true
}
// Prepare translation request
const id = getRandomNumber()
const postData: PostData = {
jsonrpc: '2.0',
method: 'LMT_handle_jobs',
id,
params: {
commonJobParams: {
mode: 'translate',
formality:
// eslint-disable-next-line sonarjs/no-nested-conditional
formal == null ? 'undefined' : formal ? 'formal' : 'informal',
transcribe_as: 'romanize',
advancedMode: false,
textType: tagHandling,
wasSpoken: false,
...(hasRegionalVariant && { regionalVariant: targetLang }),
},
lang: {
source_lang_user_selected: 'auto',
target_lang: targetLangCode,
source_lang_computed: sourceLangCode,
},
jobs,
timestamp: getTimeStamp(getICount(part)),
},
}
let translations: Translation[]
try {
// Make translation request
const { result } = await makeRequest(postData, proxyUrl, dlSession)
translations = result.translations
} catch (error) {
return {
code: HTTP_STATUS_SERVICE_UNAVAILABLE,
message: String(error),
}
}
// Process translation results
let partTranslation = ''
const partAlternatives: string[] = []
if (translations.length > 0) {
// Process main translation
for (const translation of translations) {
partTranslation += translation.beams[0].sentences[0].text + ' '
}
partTranslation = partTranslation.trim()
// Process alternatives
const numBeams = translations[0].beams.length
for (let i = 1; i < numBeams; i++) {
// Start from 1 since 0 is the main translation
let altText = ''
for (const translation of translations) {
const beams = translation.beams
if (i < beams.length) {
altText += beams[i].sentences[0].text + ' '
}
}
if (altText) {
partAlternatives.push(altText.trim())
}
}
}
if (!partTranslation) {
return {
code: HTTP_STATUS_SERVICE_UNAVAILABLE,
message: 'Translation failed',
}
}
translatedParts.push(partTranslation)
allAlternatives.push(partAlternatives)
}
// Join all translated parts with newlines
const translatedText = translatedParts.join('\n')
// Combine alternatives with proper newline handling
const combinedAlternatives: string[] = []
let maxAlts = 0
for (const alts of allAlternatives) {
if (alts.length > maxAlts) {
maxAlts = alts.length
}
}
// Create combined alternatives preserving line structure
for (let i = 0; i < maxAlts; i++) {
const altParts: string[] = []
for (const [j, alts] of allAlternatives.entries()) {
if (i < alts.length) {
altParts.push(alts[i])
} else if (translatedParts[j].length === 0) {
altParts.push('') // Keep empty lines
} else {
altParts.push(translatedParts[j]) // Use main translation if no alternative
}
}
combinedAlternatives.push(altParts.join('\n'))
}
return {
code: HTTP_STATUS_OK,
id: getRandomNumber(), // Using new ID for the complete translation
data: translatedText,
alternatives: combinedAlternatives,
sourceLang: sourceLang!,
targetLang,
method: dlSession ? 'Pro' : 'Free',
}
}