app/autotranslate/server/msTranslate.js
/**
	 * 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
	 */
Re-use method for REST API consumption of MS translate.
Re - use method for REST API consumption of MS translate .
_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) {
			translations = Object.assign({}, ...targetLanguages.map((language) =>
				({
					[language]: result.data.map((line) => line.translations.find((translation) => translation.to === language).text).join('\n'),
				}),
			));
		}
		return translations;
	}
_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 } ${ 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 ) { translations = Object . assign ( { } , ... targetLanguages . map ( ( language ) => ( { [ language ] : result . data . map ( ( line ) => line . translations . find ( ( translation ) => translation . to === language ) . text ) . join ( '\n' ) , } ) , ) ) ; } return translations ; }
Re-use method for REST API consumption of MS translate.
Re - use method for REST API consumption of MS translate .
// store translation only when the source and target language are different.