Skip to content

Commit

Permalink
[NEW] Translation via MS translate (RocketChat#16363)
Browse files Browse the repository at this point in the history
* Translation: Microsoft translate as new provider

* Translation: Display translation provider

* Linting

* Add missing translation

* Don't expose translation API keys to client

* Refactor MS translate: remove redundant code

* More precision in MS translate API key setting

* Apply suggestions from code review

Co-Authored-By: Rodrigo Nascimento <[email protected]>

* reset package-lock to upstream

Co-authored-by: Rodrigo Nascimento <[email protected]>
  • Loading branch information
rodrigok authored Mar 13, 2020
2 parents 4ec72d5 + b9bd2f6 commit 6914d11
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/autotranslate/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import './methods/saveSettings';
import './methods/translateMessage';
import './googleTranslate.js';
import './deeplTranslate.js';
import './msTranslate.js';
import './methods/getProviderUiMetadata.js';

export {
Expand Down
9 changes: 9 additions & 0 deletions app/autotranslate/server/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Logger } from '../../logger';

export const logger = new Logger('AutoTranslate', {
sections: {
google: 'Google',
deepl: 'DeepL',
microsoft: 'Microsoft',
},
});
164 changes: 164 additions & 0 deletions app/autotranslate/server/msTranslate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* @author Vigneshwaran Odayappan <[email protected]>
*/

import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { HTTP } from 'meteor/http';
import _ from 'underscore';

import { TranslationProviderRegistry, AutoTranslate } from './autotranslate';
import { logger } from './logger';
import { settings } from '../../settings';

/**
* 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 {
/**
* 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.get('AutoTranslate_MicrosoftAPIKey', (key, value) => {
this.apiKey = value;
});
}

/**
* Returns metadata information about the service provide
* @private implements super abstract method.
* @return {object}
*/
_getProviderMetadata() {
return {
name: this.name,
displayName: TAPi18n.__('AutoTranslate_Microsoft'),
settings: this._getSettings(),
};
}

/**
* Returns necessary settings information about the translation service provider.
* @private implements super abstract method.
* @return {object}
*/
_getSettings() {
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
*/
getSupportedLanguages(target) {
if (this.autoTranslateEnabled && this.apiKey) {
if (this.supportedLanguages[target]) {
return this.supportedLanguages[target];
}
const languages = HTTP.get(this.apiGetLanguages);
this.supportedLanguages[target] = Object.keys(languages.data.translation).map((language) => ({
language,
name: languages.data.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
*/
_translate(data, targetLanguages) {
let translations = {};
const supportedLanguages = this.getSupportedLanguages('en');
targetLanguages = targetLanguages.map((language) => {
if (language.indexOf('-') !== -1 && !_.findWhere(supportedLanguages, { language })) {
language = language.substr(0, 2);
}
return language;
});
const url = `${ this.apiEndPointUrl }&to=${ targetLanguages.join('&to=') }`;
const result = HTTP.post(url, {
headers: {
'Ocp-Apim-Subscription-Key': this.apiKey,
'Content-Type': 'application/json; charset=UTF-8',
},
data,
});

if (result.statusCode === 200 && result.data && result.data.length > 0) {
// store translation only when the source and target language are different.
translations = Object.assign({}, ...targetLanguages.map((language) =>
({
[language]: result.data.map((line) => 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
*/
_translateMessage(message, targetLanguages) {
// 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) {
logger.microsoft.error('Error translating message', e);
}
return {};
}

/**
* Returns translated message attachment description in target languages.
* @private
* @param {object} attachment
* @param {object} targetLanguages
* @returns {object} translated messages for each target language
*/
_translateAttachmentDescriptions(attachment, targetLanguages) {
try {
return this._translate([{
Text: attachment.description || attachment.text,
}], targetLanguages);
} catch (e) {
logger.microsoft.error('Error translating message attachment', e);
}
return {};
}
}

// Register Microsoft translation provider to the registry.
TranslationProviderRegistry.registerProvider(new MsAutoTranslate());
21 changes: 19 additions & 2 deletions app/autotranslate/server/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Meteor.startup(function() {
}, {
key: 'deepl-translate',
i18nLabel: 'AutoTranslate_DeepL',
}, {
key: 'microsoft-translate',
i18nLabel: 'AutoTranslate_Microsoft',
}],
enableQuery: [{ _id: 'AutoTranslate_Enabled', value: true }],
i18nLabel: 'AutoTranslate_ServiceProvider',
Expand All @@ -30,7 +33,7 @@ Meteor.startup(function() {
type: 'string',
group: 'Message',
section: 'AutoTranslate_Google',
public: true,
public: false,
i18nLabel: 'AutoTranslate_APIKey',
enableQuery: [
{
Expand All @@ -45,7 +48,7 @@ Meteor.startup(function() {
type: 'string',
group: 'Message',
section: 'AutoTranslate_DeepL',
public: true,
public: false,
i18nLabel: 'AutoTranslate_APIKey',
enableQuery: [
{
Expand All @@ -54,4 +57,18 @@ Meteor.startup(function() {
_id: 'AutoTranslate_ServiceProvider', value: 'deepl-translate',
}],
});

settings.add('AutoTranslate_MicrosoftAPIKey', '', {
type: 'string',
group: 'Message',
section: 'AutoTranslate_Microsoft',
public: false,
i18nLabel: 'AutoTranslate_Microsoft_API_Key',
enableQuery: [
{
_id: 'AutoTranslate_Enabled', value: true,
}, {
_id: 'AutoTranslate_ServiceProvider', value: 'microsoft-translate',
}],
});
});
4 changes: 2 additions & 2 deletions app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export class Messages extends Base {
return this.createWithTypeRoomIdMessageAndUser('r', roomId, roomName, user, extraData);
}

addTranslations(messageId, translations) {
const updateObj = {};
addTranslations(messageId, translations, providerName) {
const updateObj = { translationProvider: providerName };
Object.keys(translations).forEach((key) => {
const translation = translations[key];
updateObj[`translations.${ key }`] = translation;
Expand Down
1 change: 1 addition & 0 deletions app/ui-message/client/message.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
{{#if showTranslated}}
<span class="translated">
<i class="icon-language {{#if msg.autoTranslateFetching}}loading{{/if}}" aria-label="{{_ "Translated"}}"></i>
<span class="translation-provider">{{ translationProvider }}</span>
</span>
{{/if}}
{{#if msg.sentByEmail}}
Expand Down
6 changes: 6 additions & 0 deletions app/ui-message/client/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { t, roomTypes, getURL } from '../../utils';
import { upsertMessage } from '../../ui-utils/client/lib/RoomHistoryManager';
import './message.html';
import './messageThread.html';
import { AutoTranslate } from '../../autotranslate/client';

async function renderPdfToCanvas(canvasId, pdfLink) {
const isSafari = /constructor/i.test(window.HTMLElement)
Expand Down Expand Up @@ -250,6 +251,11 @@ Template.message.helpers({
return msg.autoTranslateFetching || (!!autoTranslate !== !!msg.autoTranslateShowInverse && msg.translations && msg.translations[settings.translateLanguage]);
}
},
translationProvider() {
const instance = Template.instance();
const { translationProvider } = instance.data.msg;
return translationProvider && AutoTranslate.providersMetadata[translationProvider].displayName;
},
edited() {
const { msg } = this;
return msg.editedAt && !MessageTypes.isSystemMessage(msg);
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@
"AutoTranslate_Enabled": "Enable Auto-Translate",
"AutoTranslate_Enabled_Description": "Enabling auto-translation will allow people with the <code class=\"inline\">auto-translate</code> permission to have all messages automatically translated into their selected language. Fees may apply.",
"AutoTranslate_Google": "Google",
"AutoTranslate_Microsoft": "Microsoft",
"AutoTranslate_Microsoft_API_Key": "Ocp-Apim-Subscription-Key",
"AutoTranslate_ServiceProvider": "Service Provider",
"Available": "Available",
"Available_agents": "Available agents",
Expand Down

0 comments on commit 6914d11

Please sign in to comment.