snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
208
apps/meteor/server/lib/autotranslate/msTranslate.ts
Normal file
208
apps/meteor/server/lib/autotranslate/msTranslate.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* @author Vigneshwaran Odayappan <vickyokrm@gmail.com>
|
||||
*/
|
||||
|
||||
import type { IMessage, IProviderMetadata, ISupportedLanguage, ITranslationResult, MessageAttachment } from '@rocket.chat/core-typings';
|
||||
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
|
||||
import _ from 'underscore';
|
||||
|
||||
import { TranslationProviderRegistry, AutoTranslate } from './autotranslate';
|
||||
import { msLogger } from './logger';
|
||||
import { settings } from '../../settings';
|
||||
import { i18n } from '../i18n';
|
||||
|
||||
/**
|
||||
* Microsoft translation service provider class representation.
|
||||
* Encapsulates the service provider settings and information.
|
||||
* Provides languages supported by the service provider.
|
||||
* Resolves API call to service provider to resolve the translation request.
|
||||
* @class
|
||||
* @augments AutoTranslate
|
||||
*/
|
||||
class MsAutoTranslate extends AutoTranslate {
|
||||
apiKey: string;
|
||||
|
||||
apiEndPointUrl: string;
|
||||
|
||||
apiDetectText: string;
|
||||
|
||||
apiGetLanguages: string;
|
||||
|
||||
breakSentence: string;
|
||||
|
||||
/**
|
||||
* setup api reference to Microsoft translate to be used as message translation provider.
|
||||
* @constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
this.name = 'microsoft-translate';
|
||||
this.apiEndPointUrl = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0';
|
||||
this.apiDetectText = 'https://api.cognitive.microsofttranslator.com/detect?api-version=3.0';
|
||||
this.apiGetLanguages = 'https://api.cognitive.microsofttranslator.com/languages?api-version=3.0';
|
||||
this.breakSentence = 'https://api.cognitive.microsofttranslator.com/breaksentence?api-version=3.0';
|
||||
// Get the service provide API key.
|
||||
settings.watch<string>('AutoTranslate_MicrosoftAPIKey', (value) => {
|
||||
this.apiKey = value;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metadata information about the service provide
|
||||
* @private implements super abstract method.
|
||||
* @return {object}
|
||||
*/
|
||||
_getProviderMetadata(): IProviderMetadata {
|
||||
return {
|
||||
name: this.name,
|
||||
displayName: i18n.t('AutoTranslate_Microsoft'),
|
||||
settings: this._getSettings(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns necessary settings information about the translation service provider.
|
||||
* @private implements super abstract method.
|
||||
* @return {object}
|
||||
*/
|
||||
_getSettings(): IProviderMetadata['settings'] {
|
||||
return {
|
||||
apiKey: this.apiKey,
|
||||
apiEndPointUrl: this.apiEndPointUrl,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns supported languages for translation by the active service provider.
|
||||
* Microsoft does not provide an endpoint yet to retrieve the supported languages.
|
||||
* So each supported languages are explicitly maintained.
|
||||
* @private implements super abstract method.
|
||||
* @param {string} target
|
||||
* @returns {object} code : value pair
|
||||
*/
|
||||
async getSupportedLanguages(target: string): Promise<ISupportedLanguage[]> {
|
||||
if (!this.apiKey) {
|
||||
return [];
|
||||
}
|
||||
if (this.supportedLanguages[target]) {
|
||||
return this.supportedLanguages[target];
|
||||
}
|
||||
// SECURITY: the URL is a default hardcoded value or an envvar/setting set by an admin. It's safe to disable this check.
|
||||
const request = await fetch(this.apiGetLanguages, {
|
||||
ignoreSsrfValidation: true,
|
||||
});
|
||||
if (!request.ok) {
|
||||
throw new Error(request.statusText);
|
||||
}
|
||||
const languages = await request.json();
|
||||
this.supportedLanguages[target] = Object.keys(languages.translation).map((language) => ({
|
||||
language,
|
||||
name: languages.translation[language].name,
|
||||
}));
|
||||
return this.supportedLanguages[target || 'en'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-use method for REST API consumption of MS translate.
|
||||
* @private
|
||||
* @param {object} message
|
||||
* @param {object} targetLanguages
|
||||
* @throws Communication Errors
|
||||
* @returns {object} translations: Translated messages for each language
|
||||
*/
|
||||
async _translate(
|
||||
data: {
|
||||
Text: string;
|
||||
}[],
|
||||
targetLanguages: string[],
|
||||
): Promise<ITranslationResult> {
|
||||
let translations: { [k: string]: string } = {};
|
||||
const supportedLanguages = await this.getSupportedLanguages('en');
|
||||
targetLanguages = targetLanguages.map((language) => {
|
||||
if (language.indexOf('-') !== -1 && !_.findWhere(supportedLanguages, { language })) {
|
||||
language = language.substr(0, 2);
|
||||
}
|
||||
return language;
|
||||
});
|
||||
// SECURITY: the URL is a default hardcoded value or an envvar/setting set by an admin. It's safe to disable this check.
|
||||
const request = await fetch(this.apiEndPointUrl, {
|
||||
ignoreSsrfValidation: true,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Ocp-Apim-Subscription-Key': this.apiKey,
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
body: data,
|
||||
params: {
|
||||
to: targetLanguages,
|
||||
},
|
||||
});
|
||||
if (!request.ok) {
|
||||
throw new Error(request.statusText);
|
||||
}
|
||||
const result = await request.json();
|
||||
|
||||
if (request.status === 200 && result.length > 0) {
|
||||
// store translation only when the source and target language are different.
|
||||
translations = Object.assign(
|
||||
{},
|
||||
...targetLanguages.map((language) => ({
|
||||
[language]: result
|
||||
.map(
|
||||
(line: { translations: { to: string; text: string }[] }) =>
|
||||
line.translations.find((translation) => translation.to === language)?.text,
|
||||
)
|
||||
.join('\n'),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
return translations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns translated message for each target language.
|
||||
* @private
|
||||
* @param {object} message
|
||||
* @param {object} targetLanguages
|
||||
* @returns {object} translations: Translated messages for each language
|
||||
*/
|
||||
async _translateMessage(message: IMessage, targetLanguages: string[]): Promise<ITranslationResult> {
|
||||
// There are multi-sentence-messages where multiple sentences come from different languages
|
||||
// This is a problem for translation services since the language detection fails.
|
||||
// Thus, we'll split the message in sentences, get them translated, and join them again after translation
|
||||
const msgs = message.msg.split('\n').map((msg) => ({ Text: msg }));
|
||||
try {
|
||||
return this._translate(msgs, targetLanguages);
|
||||
} catch (e) {
|
||||
msLogger.error({ err: e, msg: 'Error translating message' });
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns translated message attachment description in target languages.
|
||||
* @private
|
||||
* @param {object} attachment
|
||||
* @param {object} targetLanguages
|
||||
* @returns {object} translated messages for each target language
|
||||
*/
|
||||
async _translateAttachmentDescriptions(attachment: MessageAttachment, targetLanguages: string[]): Promise<ITranslationResult> {
|
||||
try {
|
||||
return this._translate(
|
||||
[
|
||||
{
|
||||
Text: attachment.description || attachment.text || '',
|
||||
},
|
||||
],
|
||||
targetLanguages,
|
||||
);
|
||||
} catch (e) {
|
||||
msLogger.error({ err: e, msg: 'Error translating message attachment' });
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// Register Microsoft translation provider to the registry.
|
||||
TranslationProviderRegistry.registerProvider(new MsAutoTranslate());
|
||||
Reference in New Issue
Block a user