Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Support for deepl free api #30433

Merged
merged 7 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading