From 38e39335042aec40437a4bbe749d7d4fcb33de45 Mon Sep 17 00:00:00 2001 From: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Date: Tue, 7 Nov 2023 13:05:33 -0300 Subject: [PATCH] chore: Support for deepl free api (#30433) --- .changeset/cool-rockets-talk.md | 5 +++++ .../autotranslate/server/deeplTranslate.ts | 21 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) 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 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);