From e0423f6e5fb97336ccb6dc7efe7a11cc02ebbdd2 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Mon, 18 Sep 2023 14:48:41 -0300 Subject: [PATCH 1/2] apikey through headers and support free api --- .../autotranslate/server/deeplTranslate.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/apps/meteor/app/autotranslate/server/deeplTranslate.ts b/apps/meteor/app/autotranslate/server/deeplTranslate.ts index 8aaed5c12d28..349133f7fd4c 100644 --- a/apps/meteor/app/autotranslate/server/deeplTranslate.ts +++ b/apps/meteor/app/autotranslate/server/deeplTranslate.ts @@ -18,6 +18,9 @@ import { SystemLogger } from '../../../server/lib/logger/system'; import { settings } from '../../settings/server'; import { TranslationProviderRegistry, AutoTranslate } from './autotranslate'; +const proApiEndpoint = 'https://api.deepl.com/v2/translate'; +const freeApiEndpoint = 'https://api-free.deepl.com/v2/translate'; + /** * DeepL translation service provider class representation. * Encapsulates the service provider settings and information. @@ -38,10 +41,18 @@ class DeeplAutoTranslate extends AutoTranslate { constructor() { super(); this.name = 'deepl-translate'; - this.apiEndPointUrl = 'https://api.deepl.com/v2/translate'; + this.apiEndPointUrl = proApiEndpoint; + // Get the service provide API key. settings.watch('AutoTranslate_DeepLAPIKey', (value) => { this.apiKey = value; + + // if the api key ends with `:fx` it is a free api key + if (/:fx$/.test(value)) { + this.apiEndPointUrl = freeApiEndpoint; + return; + } + this.apiEndPointUrl = proApiEndpoint; }); } @@ -205,7 +216,13 @@ class DeeplAutoTranslate extends AutoTranslate { language = language.substr(0, 2); } try { - const result = await fetch(this.apiEndPointUrl, { params: { auth_key: this.apiKey, target_lang: language, text: msgs } }); + const result = await fetch(this.apiEndPointUrl, { + params: { target_lang: language, text: msgs }, + headers: { + Authorization: `DeepL-Auth-Key ${this.apiKey}`, + }, + method: 'POST', + }); if (!result.ok) { throw new Error(result.statusText); From 32f7eb772c969d212635d84cf7925ff4c11f2fca Mon Sep 17 00:00:00 2001 From: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:29:43 -0300 Subject: [PATCH 2/2] add changeset --- .changeset/cool-rockets-talk.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cool-rockets-talk.md diff --git a/.changeset/cool-rockets-talk.md b/.changeset/cool-rockets-talk.md new file mode 100644 index 000000000000..8dc5af53dd88 --- /dev/null +++ b/.changeset/cool-rockets-talk.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +add support to DeepL open api