Skip to content

Commit

Permalink
chore: Support for deepl free api (#30433)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh authored Nov 7, 2023
1 parent 5b47d5e commit 38e3933
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-rockets-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

add support to DeepL open api
21 changes: 19 additions & 2 deletions apps/meteor/app/autotranslate/server/deeplTranslate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<string>('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;
});
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 38e3933

Please sign in to comment.