-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
144 lines (127 loc) · 3.53 KB
/
mod.ts
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
export const defaultOptions = {
url: "https://translate.googleapis.com/translate_a/single",
ttsUrl: "https://translate.google.com/translate_tts",
headers: {
"user-agent":
"GoogleTranslate/6.6.1.RC09.302039986 (Linux; U; Android 9; Redmi Note 8)",
},
oe: "utf-8",
ie: "utf-8",
translateClient: "gtx",
ttsClient: "at",
idx: "0",
sl: "auto",
tl: "en",
dt: "t",
dj: "1",
prev: "input",
};
export interface GTROptions {
// The full URL of text translation route
url?: string;
// The full URL of text-to-speach route
ttsUrl?: string;
// Request headers
headers?: Record<string, string>;
}
export interface TranslateOptions {
// The lanugage of the source text, pass `auto` for automatic detection.
sourceLang?: string;
// The language to translate into, default: `en` (English).
targetLang?: string;
inputEncoding?: string;
outputEncoding?: string;
dt?: string;
dj?: string;
client?: string;
}
export interface TTSOptions {
// The length of the provided text.
textlen?: string;
// The lanugage to make speech for, pass `auto` or leave empty for automatic detection.
targetLang?: string;
inputEncoding?: string;
outputEncoding?: string;
idx?: string;
prev?: string;
client?: string;
}
export interface DetectOptions {
inputEncoding?: string;
dt?: string;
dj?: string;
client?: string;
}
export interface TranslateResult {
orig: string;
trans: string;
lang: string;
}
export class GTR {
url: string;
ttsUrl: string;
headers: Record<string, string>;
constructor(opts?: GTROptions) {
this.url = opts?.url ?? defaultOptions.url;
this.ttsUrl = opts?.ttsUrl ?? defaultOptions.ttsUrl;
this.headers = opts?.headers ?? defaultOptions.headers;
}
/**
* Translates the provided text to `targetLang`.
*/
async translate(
text: string,
opts?: TranslateOptions
): Promise<TranslateResult> {
const params = new URLSearchParams({
q: text,
sl: opts?.sourceLang ?? defaultOptions.sl,
tl: opts?.targetLang ?? defaultOptions.tl,
ie: opts?.inputEncoding ?? defaultOptions.ie,
oe: opts?.outputEncoding ?? defaultOptions.oe,
dt: opts?.dt ?? defaultOptions.dt,
dj: opts?.dj ?? defaultOptions.dj,
client: opts?.client ?? defaultOptions.translateClient,
}).toString();
const response = await fetch(`${this.url}?${params}`, {
headers: this.headers,
});
const { sentences, src: lang } = await response.json();
const origRaw = [];
const transRaw = [];
for (const sentence of sentences) {
origRaw.push(sentence.orig);
transRaw.push(sentence.trans);
}
return {
orig: origRaw.join(" "),
trans: transRaw.join(" "),
lang,
};
}
/**
* Creates speech for the provided text in mp3 format.
*/
async tts(text: string, opts?: TTSOptions) {
const params = new URLSearchParams({
q: text,
textlen: opts?.textlen ?? String(text.length),
tl: opts?.targetLang ?? defaultOptions.tl,
ie: opts?.inputEncoding ?? defaultOptions.ie,
oe: opts?.outputEncoding ?? defaultOptions.oe,
idx: opts?.idx ?? defaultOptions.idx,
prev: opts?.prev ?? defaultOptions.prev,
client: opts?.client ?? defaultOptions.ttsClient,
});
const response = await fetch(`${this.ttsUrl}?${params}`, {
headers: this.headers,
});
return await response.blob();
}
/**
* Detects the language of the provided text.
*/
async detect(text: string, opts?: DetectOptions) {
return (await this.translate(text, opts)).lang;
}
}