Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13190983
colorfulclouds_patch.diff
Mountain
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
Mountain
Feb 7 2018, 1:30 PM
2018-02-07 13:30:24 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None
colorfulclouds_patch.diff
View Options
diff --git a/config.dev.yaml b/config.dev.yaml
index 39fe097..f460a60 100644
--- a/config.dev.yaml
+++ b/config.dev.yaml
@@ -87,6 +87,10 @@ services:
api: http://matxin.elhuyar.eus/API
languages: config/Matxin.yaml
key: null
+ ColorfulClouds:
+ api: https://api.interpreter.caiyunai.com
+ languages: config/ColorfulClouds.yaml
+ key: null
dictionary:
Dictd:
languages: config/Dictd.yaml
diff --git a/config/ColorfulClouds.yaml b/config/ColorfulClouds.yaml
index e69de29..a0914b0 100644
--- a/config/ColorfulClouds.yaml
+++ b/config/ColorfulClouds.yaml
@@ -0,0 +1,7 @@
+en:
+ - zh
+ja:
+ - zh
+zh:
+ - ja
+ - en
diff --git a/config/mt-defaults.wikimedia.yaml b/config/mt-defaults.wikimedia.yaml
index f0537a0..10f9629 100644
--- a/config/mt-defaults.wikimedia.yaml
+++ b/config/mt-defaults.wikimedia.yaml
@@ -981,3 +981,9 @@
'is-sv': Apertium
'kk-tt': Apertium
'hi-ur': Apertium
+'en-zh': ColorfulClouds
+'ja-zh': ColorfulClouds
+'zh-en': ColorfulClouds
+'zh-ja': ColorfulClouds
+
+
diff --git a/lib/mt/ColorfulClouds.js b/lib/mt/ColorfulClouds.js
index e69de29..76208c5 100644
--- a/lib/mt/ColorfulClouds.js
+++ b/lib/mt/ColorfulClouds.js
@@ -0,0 +1,100 @@
+'use strict';
+
+const preq = require( 'preq' ),
+ MTClient = require( './MTClient.js' );
+
+class ColorfulClouds extends MTClient {
+
+ /**
+ * Translate html or plain text content with Yandex.
+ * Yandex 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
+ */
+ translateText( sourceLang, targetLang, sourceText ) {
+ var key, postData;
+
+ key = this.conf.mt.ColorfulClouds.key;
+ if ( key === null ) {
+ return Promise.reject( new Error( 'ColorfulClouds service is misconfigured' ) );
+ }
+
+ if ( sourceText.length > 10000 ) {
+ return Promise.reject( new Error( 'Source text too long: ' +
+ sourceLang + '-' + targetLang ) );
+ }
+
+ postData = {
+ uri: this.conf.mt.ColorfulClouds.api + '/v1/translator',
+ headers: {
+ 'x-authorization': 'token ' + key,
+ 'cache-control': 'no-cache',
+ 'content-type': 'application/json'
+ },
+ body: {
+ request_id: 'wikimedia', replaced: true, cached: true,
+ trans_type: sourceLang + '2' + targetLang,
+ source: [ sourceText ]
+ }
+ };
+
+ return preq.post( postData )
+ .then( ( response ) => {
+ var target, body = response.body;
+ if ( 'target' in body ) {
+ target = response.body.target;
+ if ( target instanceof Array ) {
+ return target.join( '' );
+ } else {
+ return target;
+ }
+ } else {
+ if ( response.status === 200 ) {
+ throw new Error( 'Translation with ColorfulClouds failed. Error: ' +
+ this.getErrorName( response.body.error ) +
+ ` for ${sourceLang} + '>' + ${targetLang}: ` );
+ } else {
+ throw new Error( 'Translation with ColorfulClouds failed. Error: ' +
+ this.getErrorName( response.status ) +
+ ` for ${sourceLang} + '>' + ${targetLang}: ` );
+ }
+ }
+ } );
+ }
+
+ /**
+ * Returns error name from error code.
+ *
+ * @param {number} code Error code
+ * @return {string}
+ */
+ getErrorName( code ) {
+ const errormap = {
+ 200: 'ERR_OK',
+ 401: 'ERR_KEY_INVALID',
+ 402: 'ERR_KEY_BLOCKED',
+ 403: 'ERR_DAILY_REQ_LIMIT_EXCEEDED',
+ 404: 'ERR_DAILY_CHAR_LIMIT_EXCEEDED',
+ 413: 'ERR_TEXT_TOO_LONG',
+ 422: 'ERR_UNPROCESSABLE_TEXT',
+ 501: 'ERR_LANG_NOT_SUPPORTED'
+ };
+
+ if ( code in errormap ) {
+ return errormap[ code ];
+ }
+
+ return `${code}`;
+ }
+
+ requiresAuthorization() {
+ return false;
+ }
+}
+
+module.exports = ColorfulClouds;
diff --git a/lib/mt/index.js b/lib/mt/index.js
index d05029a..25f1415 100644
--- a/lib/mt/index.js
+++ b/lib/mt/index.js
@@ -5,5 +5,6 @@ module.exports = {
Matxin: require( './Matxin' ),
TestClient: require( './TestClient' ),
Yandex: require( './Yandex' ),
- Youdao: require( './Youdao' )
+ Youdao: require( './Youdao' ),
+ ColorfulClouds: require( './ColorfulClouds' )
};
diff --git a/spec.yaml b/spec.yaml
index 1cfdd81..085ca22 100644
--- a/spec.yaml
+++ b/spec.yaml
@@ -180,6 +180,7 @@ paths:
enum:
- Apertium
- Matxin
+ - ColorfulClouds
- name: html
in: formData
description: The HTML or plaintext content to translate
@@ -335,6 +336,7 @@ paths:
required: false
enum:
- Apertium
+ - ColorfulClouds
- name: html
in: formData
description: The HTML content to translate
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5486101
Default Alt Text
colorfulclouds_patch.diff (4 KB)
Attached To
Mode
T186715: Add new MT Client for LingoCloud
Attached
Detach File
Event Timeline
Log In to Comment