|
| 1 | +/** |
| 2 | + * @author Vigneshwaran Odayappan <[email protected]> |
| 3 | + */ |
| 4 | + |
| 5 | +import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
| 6 | +import { HTTP } from 'meteor/http'; |
| 7 | +import _ from 'underscore'; |
| 8 | + |
| 9 | +import { TranslationProviderRegistry, AutoTranslate } from './autotranslate'; |
| 10 | +import { logger } from './logger'; |
| 11 | +import { settings } from '../../settings'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Microsoft translation service provider class representation. |
| 15 | + * Encapsulates the service provider settings and information. |
| 16 | + * Provides languages supported by the service provider. |
| 17 | + * Resolves API call to service provider to resolve the translation request. |
| 18 | + * @class |
| 19 | + * @augments AutoTranslate |
| 20 | + */ |
| 21 | +class MsAutoTranslate extends AutoTranslate { |
| 22 | + /** |
| 23 | + * setup api reference to Microsoft translate to be used as message translation provider. |
| 24 | + * @constructor |
| 25 | + */ |
| 26 | + constructor() { |
| 27 | + super(); |
| 28 | + this.name = 'microsoft-translate'; |
| 29 | + this.apiEndPointUrl = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0'; |
| 30 | + this.apiDetectText = 'https://api.cognitive.microsofttranslator.com/detect?api-version=3.0'; |
| 31 | + this.apiGetLanguages = 'https://api.cognitive.microsofttranslator.com/languages?api-version=3.0'; |
| 32 | + this.breakSentence = 'https://api.cognitive.microsofttranslator.com/breaksentence?api-version=3.0'; |
| 33 | + // Get the service provide API key. |
| 34 | + settings.get('AutoTranslate_MicrosoftAPIKey', (key, value) => { |
| 35 | + this.apiKey = value; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns metadata information about the service provide |
| 41 | + * @private implements super abstract method. |
| 42 | + * @return {object} |
| 43 | + */ |
| 44 | + _getProviderMetadata() { |
| 45 | + return { |
| 46 | + name: this.name, |
| 47 | + displayName: TAPi18n.__('AutoTranslate_Microsoft'), |
| 48 | + settings: this._getSettings(), |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns necessary settings information about the translation service provider. |
| 54 | + * @private implements super abstract method. |
| 55 | + * @return {object} |
| 56 | + */ |
| 57 | + _getSettings() { |
| 58 | + return { |
| 59 | + apiKey: this.apiKey, |
| 60 | + apiEndPointUrl: this.apiEndPointUrl, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns supported languages for translation by the active service provider. |
| 66 | + * Microsoft does not provide an endpoint yet to retrieve the supported languages. |
| 67 | + * So each supported languages are explicitly maintained. |
| 68 | + * @private implements super abstract method. |
| 69 | + * @param {string} target |
| 70 | + * @returns {object} code : value pair |
| 71 | + */ |
| 72 | + getSupportedLanguages(target) { |
| 73 | + if (this.autoTranslateEnabled && this.apiKey) { |
| 74 | + if (this.supportedLanguages[target]) { |
| 75 | + return this.supportedLanguages[target]; |
| 76 | + } |
| 77 | + const languages = HTTP.get(this.apiGetLanguages); |
| 78 | + this.supportedLanguages[target] = Object.keys(languages.data.translation).map((language) => ({ |
| 79 | + language, |
| 80 | + name: languages.data.translation[language].name, |
| 81 | + })); |
| 82 | + return this.supportedLanguages[target || 'en']; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Re-use method for REST API consumption of MS translate. |
| 88 | + * @private |
| 89 | + * @param {object} message |
| 90 | + * @param {object} targetLanguages |
| 91 | + * @throws Communication Errors |
| 92 | + * @returns {object} translations: Translated messages for each language |
| 93 | + */ |
| 94 | + _translate(data, targetLanguages) { |
| 95 | + let translations = {}; |
| 96 | + const supportedLanguages = this.getSupportedLanguages('en'); |
| 97 | + targetLanguages = targetLanguages.map((language) => { |
| 98 | + if (language.indexOf('-') !== -1 && !_.findWhere(supportedLanguages, { language })) { |
| 99 | + language = language.substr(0, 2); |
| 100 | + } |
| 101 | + return language; |
| 102 | + }); |
| 103 | + const url = `${ this.apiEndPointUrl }&to=${ targetLanguages.join('&to=') }`; |
| 104 | + const result = HTTP.post(url, { |
| 105 | + headers: { |
| 106 | + 'Ocp-Apim-Subscription-Key': this.apiKey, |
| 107 | + 'Content-Type': 'application/json; charset=UTF-8', |
| 108 | + }, |
| 109 | + data, |
| 110 | + }); |
| 111 | + |
| 112 | + if (result.statusCode === 200 && result.data && result.data.length > 0) { |
| 113 | + // store translation only when the source and target language are different. |
| 114 | + translations = Object.assign({}, ...targetLanguages.map((language) => |
| 115 | + ({ |
| 116 | + [language]: result.data.map((line) => line.translations.find((translation) => translation.to === language).text).join('\n'), |
| 117 | + }), |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + return translations; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns translated message for each target language. |
| 126 | + * @private |
| 127 | + * @param {object} message |
| 128 | + * @param {object} targetLanguages |
| 129 | + * @returns {object} translations: Translated messages for each language |
| 130 | + */ |
| 131 | + _translateMessage(message, targetLanguages) { |
| 132 | + // There are multi-sentence-messages where multiple sentences come from different languages |
| 133 | + // This is a problem for translation services since the language detection fails. |
| 134 | + // Thus, we'll split the message in sentences, get them translated, and join them again after translation |
| 135 | + const msgs = message.msg.split('\n').map((msg) => ({ Text: msg })); |
| 136 | + try { |
| 137 | + return this._translate(msgs, targetLanguages); |
| 138 | + } catch (e) { |
| 139 | + logger.microsoft.error('Error translating message', e); |
| 140 | + } |
| 141 | + return {}; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns translated message attachment description in target languages. |
| 146 | + * @private |
| 147 | + * @param {object} attachment |
| 148 | + * @param {object} targetLanguages |
| 149 | + * @returns {object} translated messages for each target language |
| 150 | + */ |
| 151 | + _translateAttachmentDescriptions(attachment, targetLanguages) { |
| 152 | + try { |
| 153 | + return this._translate([{ |
| 154 | + Text: attachment.description || attachment.text, |
| 155 | + }], targetLanguages); |
| 156 | + } catch (e) { |
| 157 | + logger.microsoft.error('Error translating message attachment', e); |
| 158 | + } |
| 159 | + return {}; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// Register Microsoft translation provider to the registry. |
| 164 | +TranslationProviderRegistry.registerProvider(new MsAutoTranslate()); |
0 commit comments