-
Notifications
You must be signed in to change notification settings - Fork 5
/
DeepL.gs
248 lines (225 loc) · 9.66 KB
/
DeepL.gs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
MIT License
Copyright 2022 DeepL SE (https://www.deepl.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Please change the line below */
const authKey = "b493b8ef-0176-215d-82fe-e28f182c9544:fx"; // Replace with your authentication key
/* Change the line below to disable all translations. */
const disableTranslations = false; // Set to true to stop translations.
/* Change the line below to activate auto-detection of re-translations. */
const activateAutoDetect = false; // Set to true to enable auto-detection of re-translation.
/* You shouldn't need to modify the lines below here */
/**
* Translates from one language to another using the DeepL Translation API.
*
* Note that you need to set your DeepL auth key by calling DeepLAuthKey() before use.
*
* @param {"Hello"} input The text to translate.
* @param {"en"} sourceLang Optional. The language code of the source language.
* Use "auto" to auto-detect the language.
* @param {"es"} targetLang Optional. The language code of the target language.
* If unspecified, defaults to your system language.
* @param {"def3a26b-3e84-..."} glossaryId Optional. The ID of a glossary to use
* for the translation.
* @param {cell range} options Optional. Range of additional options to send with API translation
* request. May also be specified inline e.g. '{"tag_handling", "xml"; "ignore_tags", "ignore"}'
* @return Translated text.
* @customfunction
*/
function DeepLTranslate(input,
sourceLang,
targetLang,
glossaryId,
options
) {
if (input === undefined) {
throw new Error("input field is undefined, please specify the text to translate.");
} else if (typeof input === "number") {
input = input.toString();
} else if (typeof input !== "string") {
throw new Error("input text must be a string.");
}
// Check the current cell to detect recalculations due to reopening the sheet
const cell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getCurrentCell();
if (disableTranslations) {
Logger.log("disableTranslations is active, skipping DeepL translation request");
return cell.getDisplayValue();
}
if (activateAutoDetect &&
cell.getDisplayValue() !== "" &&
cell.getDisplayValue() !== "Loading...") {
Logger.log("Detected cell-recalculation, skipping DeepL translation request");
return cell.getDisplayValue();
}
if (!targetLang) targetLang = selectDefaultTargetLang_();
let formData = {
'target_lang': targetLang,
'text': input
};
if (sourceLang && sourceLang !== 'auto') {
formData['source_lang'] = sourceLang;
}
if (glossaryId) {
formData['glossary_id'] = glossaryId;
}
if (options) {
if (!Array.isArray(options) ||
!Object.values(options).every(function(value) {
return Array.isArray(value) && value.length === 2;
})) {
throw new Error("options must be a range with two columns, or have the form '{\"opt1\", \"val1\"; \"opt2\", \"val2\"}'");
}
for (let i = 0; i < options.length; i++) {
const items = options[i];
const key = items[0];
const value = items[1];
formData[key] = value;
}
}
const response = httpRequestWithRetries_('post', '/v2/translate', formData, input.length);
checkResponse_(response);
const responseObject = JSON.parse(response.getContentText());
return responseObject.translations[0].text;
}
/**
* Retrieve information about your DeepL API usage during the current billing period.
* @param {"count", "limit"} type Optional, retrieve the current used amount ("count")
* or the maximum allowed amount ("limit").
* @return String explaining usage, or count or limit values as specified by type argument.
* @customfunction
*/
function DeepLUsage(type) {
const response = httpRequestWithRetries_('get', '/v2/usage');
checkResponse_(response);
const responseObject = JSON.parse(response.getContentText());
const charCount = responseObject.character_count;
const charLimit = responseObject.character_limit;
if (charCount === undefined || charLimit === undefined)
throw new Error('Character usage not found.');
if (type) {
if (type === 'count') return charCount;
if (type === 'limit') return charLimit;
throw new Error('Unrecognized type argument.');
}
return `${charCount} of ${charLimit} characters used.`;
}
/////////////////////////////////////////////////////////////////////////////////////////
// General helper functions
/////////////////////////////////////////////////////////////////////////////////////////
/**
* Determines the default target language using the system language.
* @return A DeepL-supported target language.
* @throws Error If the system language could not be converted to a supported target language.
*/
function selectDefaultTargetLang_() {
const targetLangs = [
'bg', 'cs', 'da', 'de', 'el', 'en-gb', 'en-us', 'es', 'et', 'fi', 'fr', 'hu', 'id',
'it', 'ja', 'lt', 'lv', 'nl', 'pl', 'pt-br', 'pt-pt', 'ro', 'ru', 'sk', 'sl', 'sv',
'tr', 'zh'];
const locale = Session.getActiveUserLocale().replace('_', '-').toLowerCase();
if (targetLangs.indexOf(locale) !== -1) return locale;
const localePrefix = locale.substring(0, 2);
if (targetLangs.indexOf(localePrefix) !== -1) return localePrefix;
if (localePrefix === 'en') return 'en-US';
if (localePrefix === 'pt') return 'en-PT';
return 'en';
}
/**
* Helper function to check response code is OK and if not, throw useful exceptions.
*/
function checkResponse_(response) {
const responseCode = response.getResponseCode();
if (200 <= responseCode && responseCode < 400) return;
const content = response.getContentText();
let message = '';
try {
const jsonObj = JSON.parse(content);
if (jsonObj.message !== undefined) {
message += `, message: ${jsonObj.message}`;
}
if (jsonObj.detail !== undefined) {
message += `, detail: ${jsonObj.detail}`;
}
} catch (error) {
// JSON parsing errors are ignored, and we fall back to the raw content
message = ', ' + content;
}
switch (responseCode) {
case 403:
throw new Error(`Authorization failure, check authKey${message}`);
case 456:
throw new Error(`Quota for this billing period has been exceeded${message}`);
case 400:
throw new Error(`Bad request${message}`);
case 429:
throw new Error(
`Too many requests, DeepL servers are currently experiencing high load${message}`,
);
default: {
throw new Error(
`Unexpected status code: ${responseCode} ${message}, content: ${content}`,
);
}
}
}
/**
* Helper function to execute HTTP requests and retry failed requests.
*/
function httpRequestWithRetries_(method, relativeUrl, formData = null, charCount = 0) {
const baseUrl = authKey.endsWith(':fx')
? 'https://api-free.deepl.com'
: 'https://api.deepl.com';
const url = baseUrl + relativeUrl;
const params = {
method: method,
muteHttpExceptions: true,
headers: {
'Authorization': 'DeepL-Auth-Key ' + authKey,
},
};
if (formData) params.payload = formData;
let response = null;
for (let numRetries = 0; numRetries < 5; numRetries++) {
const lastRequestTime = Date.now();
try {
Logger.log(`Sending HTTP request to ${url} with ${charCount} characters`);
response = UrlFetchApp.fetch(url, params);
const responseCode = response.getResponseCode();
if (responseCode !== 429 && responseCode < 500) {
return response;
}
} catch (e) {
// It would be sensible to check whether the exception is retryable here, but there is
// not so much documentation on Google Apps Script exceptions. In addition, UrlFetchApp
// fetch timeouts are very long and not configurable.
throw e;
}
Logger.log(`Retrying after ${numRetries} failed requests.`);
sleepForBackoff(numRetries, lastRequestTime);
}
return response;
}
/**
* Helper function to sleep after failed requests.
*/
function sleepForBackoff(numRetries, lastRequestTime) {
const backoff = Math.min(1000 * (1.6 ** numRetries), 60000);
const jitter = 1 + 0.23 * (2 * Math.random() - 1); // Random value in [0.77 1.23]
const sleepTime = Date.now() - lastRequestTime + backoff * jitter;
Utilities.sleep(sleepTime);
}