Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate: allow setting format of text #1647

Merged
merged 2 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"arrify": "^1.0.0",
"extend": "^3.0.0",
"is": "^3.0.1",
"is-html": "^1.0.0",
"propprop": "^0.3.0"
},
"devDependencies": {
Expand Down
9 changes: 8 additions & 1 deletion packages/translate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var arrify = require('arrify');
var common = require('@google-cloud/common');
var extend = require('extend');
var is = require('is');
var isHtml = require('is-html');
var prop = require('propprop');

var PKG = require('../package.json');
Expand Down Expand Up @@ -268,6 +269,9 @@ Translate.prototype.getLanguages = function(target, callback) {
* target ISO 639-1 language code to translate the source input to. (e.g.
* `en` for English). If an object, you may also specify the source
* language.
* @param {string} options.format - Set the text's format as `html` or `text`.

This comment was marked as spam.

This comment was marked as spam.

* If not provided, we will try to auto-detect if the text given is HTML. If
* not, we set the format as `text`.
* @param {string} options.from - The ISO 639-1 language code the source input
* is written in.
* @param {string} options.to - The ISO 639-1 language code to translate the
Expand Down Expand Up @@ -323,8 +327,11 @@ Translate.prototype.getLanguages = function(target, callback) {
* });
*/
Translate.prototype.translate = function(input, options, callback) {
input = arrify(input);

var query = {
q: arrify(input)
q: input,
format: options.format || (isHtml(input[0]) ? 'html' : 'text')
};

if (is.string(options)) {
Expand Down
20 changes: 20 additions & 0 deletions packages/translate/system-test/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ var API_KEY = process.env.GCLOUD_TESTS_API_KEY;
done();
});
});

it('should autodetect HTML', function(done) {
var input = '<body>' + INPUT[0].input + '</body>';
var expectedTranslation = [
'<body>',
INPUT[0].expectedTranslation,
'</body>'
].join(' ');

var opts = {
from: 'en',
to: 'es'
};

translate.translate(input, opts, function(err, results) {
assert.ifError(err);
assert.strictEqual(results, expectedTranslation);
done();
});
});
});

describe('supported languages', function() {
Expand Down
35 changes: 35 additions & 0 deletions packages/translate/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ describe('Translate', function() {

describe('translate', function() {
var INPUT = 'Hello';
var INPUT_HTML = '<html><body>Hello</body></html>';
var SOURCE_LANG_CODE = 'en';
var TARGET_LANG_CODE = 'es';

Expand Down Expand Up @@ -306,12 +307,46 @@ describe('Translate', function() {
});
});

describe('options.format', function() {
it('should default to text', function(done) {
translate.request = function(reqOpts) {
assert.strictEqual(reqOpts.qs.format, 'text');
done();
};

translate.translate(INPUT, OPTIONS, assert.ifError);
});

it('should recognize HTML', function(done) {
translate.request = function(reqOpts) {
assert.strictEqual(reqOpts.qs.format, 'html');
done();
};

translate.translate(INPUT_HTML, OPTIONS, assert.ifError);
});

it('should allow overriding the format', function(done) {
var options = extend({}, OPTIONS, {
format: 'custom format'
});

translate.request = function(reqOpts) {
assert.strictEqual(reqOpts.qs.format, options.format);
done();
};

translate.translate(INPUT_HTML, options, assert.ifError);
});
});

it('should make the correct API request', function(done) {
translate.request = function(reqOpts) {
assert.strictEqual(reqOpts.uri, '');
assert.strictEqual(reqOpts.useQuerystring, true);
assert.deepEqual(reqOpts.qs, {
q: [INPUT],
format: 'text',
source: SOURCE_LANG_CODE,
target: TARGET_LANG_CODE
});
Expand Down