Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/306652
Change subject: MT: Google Translate client ...................................................................... MT: Google Translate client Bug: T90208 Change-Id: I821610da2c470a0954688177ed83d5e0417c05a2 --- M config.dev.yaml M config.prod.yaml A mt/Google.js M mt/index.js M registry.yaml 5 files changed, 360 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver refs/changes/52/306652/1 diff --git a/config.dev.yaml b/config.dev.yaml index f17e684..dd3f178 100644 --- a/config.dev.yaml +++ b/config.dev.yaml @@ -62,6 +62,9 @@ # Apertium web API URL apertium: api: http://apertium.wmflabs.org + google: + api: https://www.googleapis.com/language/translate/v2 + key: null yandex: api: https://translate.yandex.net key: null diff --git a/config.prod.yaml b/config.prod.yaml index 362ab26..6513e7a 100644 --- a/config.prod.yaml +++ b/config.prod.yaml @@ -62,6 +62,9 @@ # Apertium web API URL apertium: api: http://apertium.wmflabs.org + google: + api: https://www.googleapis.com/language/translate/v2 + key: null yandex: api: https://translate.yandex.net key: null diff --git a/mt/Google.js b/mt/Google.js new file mode 100644 index 0000000..279fba5 --- /dev/null +++ b/mt/Google.js @@ -0,0 +1,80 @@ +'use strict'; + +var + util = require( 'util' ), + preq = require( 'preq' ), + BBPromise = require( 'bluebird' ), + MTClient = require( './MTClient.js' ), + googleLanguageNameMap; + +// Google language codes can differ from the language codes that +// we use. +googleLanguageNameMap = { + 'be-tarask': 'be', // T122033 + nb: 'no' // T132217 +}; + +function Google( options ) { + this.logger = options.logger; + this.conf = options.conf; +} + +util.inherits( Google, MTClient ); + +/** + * Translate html or plain text content with Google. + * Google is capable of translating plain text and html with + * annotations mapping (keeps markup retained in translated content). + * Hence overriding translate method of MTClient. + * + * @param {string} sourceLang Source language code + * @param {string} targetLang Target language code + * @param {string} sourceText Source language text + * @return {Q.Promise} Target language text + */ +Google.prototype.translate = function ( sourceLang, targetLang, sourceText ) { + var key, postData; + + key = this.conf.mt.google.key; + if ( key === null ) { + return BBPromise.reject( new Error( 'Google service is misconfigured' ) ); + } + + if ( sourceText.length > 5000 ) { + // Max limit is 5K characters for Google. + return BBPromise.reject( new Error( 'Source text too long' ) ); + } + + sourceLang = googleLanguageNameMap[ sourceLang ] || sourceLang; + targetLang = googleLanguageNameMap[ targetLang ] || targetLang; + // See https://cloud.google.com/translate/v2/translating-text-with-rest + postData = { + uri: this.conf.mt.google.api, + proxy: this.conf.proxy, + body: { + key: key, + source: sourceLang, + target: targetLang, + format: 'html', + q: sourceText + }, + headers: { + // To use POST, we must use the X-HTTP-Method-Override header + // to tell the Translate API to treat the request as a GET + 'X-HTTP-Method-Override': 'GET' + } + }; + + return preq.post( postData ).then( function ( response ) { + return response.body.data.translations[ 0 ].translatedText; + } ).catch( function ( response ) { + throw new Error( 'Translation with Google failed. Error: ' + + response.body.error.code + ': ' + response.body.error.message ); + } ); +}; + +Google.prototype.requiresAuthorization = function () { + return true; +}; + +module.exports = Google; diff --git a/mt/index.js b/mt/index.js index 14ca54c..c71113b 100644 --- a/mt/index.js +++ b/mt/index.js @@ -1,5 +1,6 @@ module.exports = { Apertium: require( './Apertium.js' ), + Google: require( './Google.js' ), Yandex: require( './Yandex.js' ), - Youdao: require( './Youdao.js' ) + Youdao: require( './Youdao.js' ) }; diff --git a/registry.yaml b/registry.yaml index 4f67cad..3e003fe 100644 --- a/registry.yaml +++ b/registry.yaml @@ -677,6 +677,278 @@ - kk ur: - hi + Google: + en: + - af + - ar + - hy + - az + - eu + - be + - bn + - bs + - bg + - ca + - ceb + - ny + - zh + - hr + - cs + - sq + - da + - nl + - eo + - et + - tl + - fi + - fr + - gl + - ka + - de + - el + - gu + - ht + - ha + - iw + - hi + - hmn + - hu + - is + - ig + - id + - ga + - it + - ja + - jw + - kn + - kk + - km + - ko + - lo + - la + - lv + - lt + - mk + - mg + - ms + - ml + - mt + - mi + - mr + - mn + - my + - ne + - no + - fa + - pl + - pt + - ma + - ro + - ru + - sr + - st + - si + - sk + - sl + - so + - es + - su + - sw + - sv + - tg + - ta + - te + - th + - tr + - uk + - ur + - uz + - vi + - cy + - yi + - yo + - zu + es: + - af + - ar + - hy + - az + - eu + - be + - bn + - bs + - bg + - ca + - ceb + - en + - ny + - zh + - hr + - cs + - sq + - da + - nl + - eo + - et + - tl + - fi + - fr + - gl + - ka + - de + - el + - gu + - ht + - ha + - iw + - hi + - hmn + - hu + - is + - ig + - id + - ga + - it + - ja + - jw + - kn + - kk + - km + - ko + - lo + - la + - lv + - lt + - mk + - mg + - ms + - ml + - mt + - mi + - mr + - mn + - my + - ne + - no + - fa + - pl + - pt + - ma + - ro + - ru + - sr + - st + - si + - sk + - sl + - so + - su + - sw + - sv + - tg + - ta + - te + - th + - tr + - uk + - ur + - uz + - vi + - cy + - yi + - yo + - zu + fr: + - af + - ar + - hy + - az + - eu + - be + - bn + - bs + - bg + - ca + - ceb + - en + - ny + - zh + - hr + - cs + - sq + - da + - nl + - eo + - et + - tl + - fi + - fr + - gl + - ka + - de + - el + - gu + - ht + - ha + - iw + - hi + - hmn + - hu + - is + - ig + - id + - ga + - it + - ja + - jw + - kn + - kk + - km + - ko + - lo + - la + - lv + - lt + - mk + - mg + - ms + - ml + - mt + - mi + - mr + - mn + - my + - ne + - no + - fa + - pl + - pt + - ma + - ro + - ru + - sr + - st + - si + - sk + - sl + - so + - es + - su + - sw + - sv + - tg + - ta + - te + - th + - tr + - uk + - ur + - uz + - vi + - cy + - yi + - yo + - zu Yandex: af: - ba -- To view, visit https://gerrit.wikimedia.org/r/306652 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I821610da2c470a0954688177ed83d5e0417c05a2 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/cxserver Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits