app/autotranslate/server/googleTranslate.js
/**
	 * Send Request REST API call to the service provider.
	 * Returns translated message for each target language in target languages.
	 * @private
	 * @param {object} message
	 * @param {object} targetLanguages
	 * @returns {object} translations: Translated messages for each language
	 */
Send Request REST API call to the service provider.
Returns translated message for each target language in target languages.
@private
@param {object} message
@param {object} targetLanguages
@returns {object} translations: Translated messages for each language
Send Request REST API call to the service provider . Returns translated message for each target language in target languages . @private @param { object } message @param { object } targetLanguages @returns { object } translations : Translated messages for each language
_translateMessage(message, targetLanguages) {
		const translations = {};
		let msgs = message.msg.split('\n');
		msgs = msgs.map((msg) => encodeURIComponent(msg));
		const query = `q=${ msgs.join('&q=') }`;
		const supportedLanguages = this.getSupportedLanguages('en');
		targetLanguages.forEach((language) => {
			if (language.indexOf('-') !== -1 && !_.findWhere(supportedLanguages, { language })) {
				language = language.substr(0, 2);
			}
			try {
				const result = HTTP.get(this.apiEndPointUrl, {
					params: {
						key: this.apiKey,
						target: language,
					},
					query,
				});
				if (result.statusCode === 200 && result.data && result.data.data && result.data.data.translations && Array.isArray(result.data.data.translations) && result.data.data.translations.length > 0) {
					const txt = result.data.data.translations.map((translation) => translation.translatedText).join('\n');
					translations[language] = this.deTokenize(Object.assign({}, message, { msg: txt }));
				}
			} catch (e) {
				SystemLogger.error('Error translating message', e);
			}
		});
		return translations;
	}
_translateMessage ( message , targetLanguages ) { const translations = { } ; let msgs = message . msg . split ( '\n' ) ; msgs = msgs . map ( ( msg ) => encodeURIComponent ( msg ) ) ; const query = ` ${ msgs . join ( '&q=' ) } ` ; const supportedLanguages = this . getSupportedLanguages ( 'en' ) ; targetLanguages . forEach ( ( language ) => { if ( language . indexOf ( '-' ) !== - 1 && ! _ . findWhere ( supportedLanguages , { language } ) ) { language = language . substr ( 0 , 2 ) ; } try { const result = HTTP . get ( this . apiEndPointUrl , { params : { key : this . apiKey , target : language , } , query , } ) ; if ( result . statusCode === 200 && result . data && result . data . data && result . data . data . translations && Array . isArray ( result . data . data . translations ) && result . data . data . translations . length > 0 ) { const txt = result . data . data . translations . map ( ( translation ) => translation . translatedText ) . join ( '\n' ) ; translations [ language ] = this . deTokenize ( Object . assign ( { } , message , { msg : txt } ) ) ; } } catch ( e ) { SystemLogger . error ( 'Error translating message' , e ) ; } } ) ; return translations ; }
Send Request REST API call to the service provider.
Send Request REST API call to the service provider .