Skip to content

Commit

Permalink
Add remaining Translate samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 30, 2016
1 parent 241d28f commit cf836ca
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 47 deletions.
26 changes: 14 additions & 12 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ __Usage:__ `node translate --help`

```
Commands:
detect <text> Detect the language of the provided text
list List available translation languages.
translate <text> Translate the provided text to the target language.
detect <input..> Detect the language of the provided text
list [target] List available translation languages.
translate <input..> Translate the provided text to the target language.
Options:
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment
variable. [string]
--help Show help [boolean]
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.
[string] [default: "AIzaSyCxFhP0edrbIJs41_TeMSHH3V2dzWeC_NY"]
--help Show help [boolean]
Examples:
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
node translate list -k your-key List available translation languages.
node translate translate -k your-key --to ru "Good Translate "Good morning!" to Russian,
morning!" auto-detecting English.
node translate translate -k your-key --to ru Translate "Good morning!" to Russian from
--from en "Good morning!" English.
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
node translate list -k your-key List available translation languages.
node translate list es -k your-key List available translation languages with names in
Spanish.
node translate translate -k your-key --to ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting
English.
node translate translate -k your-key --to ru --from en "Good Translate "Good morning!" to Russian from English.
morning!"
For more information, see https://cloud.google.com/translate/docs
```
Expand Down
2 changes: 1 addition & 1 deletion translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"@google-cloud/translate": "^0.1.1",
"@google-cloud/translate": "^0.2.0",
"iso-639-1": "^1.2.1",
"yargs": "^5.0.0"
},
Expand Down
16 changes: 14 additions & 2 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('translate:translate', function () {
assert.ifError(err);
assert(result, 'should have received a result');
assert.equal(result.language, 'en', 'should have detected english');
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
assert(console.log.calledWith('Detected language:', result));
done();
});
});
Expand All @@ -46,10 +46,22 @@ describe('translate:translate', function () {
});
});

describe('listLanguageNames', function () {
it('should list languages with names in target language', function (done) {
program.listLanguageNames('es', apiKey, function (err, languages) {
assert.ifError(err);
assert(Array.isArray(languages));
assert(languages.length > 0);
assert(console.log.calledWith('Found %d language(s)!', languages.length));
done();
});
});
});

describe('translateText', function () {
it('should translate text', function (done) {
var options = {
text: text,
input: text,
apiKey: apiKey,
to: 'ru'
};
Expand Down
63 changes: 52 additions & 11 deletions translate/test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var proxyquire = require('proxyquire').noCallThru();
var text = 'Hello world!';
var apiKey = 'key';
var target = 'es';

function getSample () {
var languagesMock = [
Expand All @@ -29,9 +30,9 @@ function getSample () {
};
var translationMock = 'Привет мир!';
var translateMock = {
getLanguages: sinon.stub().callsArgWith(0, null, languagesMock),
detect: sinon.stub().callsArgWith(1, null, resultMock),
translate: sinon.stub().callsArgWith(2, null, translationMock)
getLanguages: sinon.stub().yields(null, languagesMock),
detect: sinon.stub().yields(null, resultMock),
translate: sinon.stub().yields(null, translationMock)
};
var TranslateMock = sinon.stub().returns(translateMock);

Expand Down Expand Up @@ -65,14 +66,14 @@ describe('translate:translate', function () {
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result');
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', sample.mocks.result.confidence));
assert(console.log.calledWith('Detected language:', sample.mocks.result));
});

it('should handle error', function () {
var error = new Error('error');
var sample = getSample();
var callback = sinon.stub();
sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error);
sample.mocks.translate.detect.yields(error);

sample.program.detectLanguage(text, apiKey, callback);

Expand Down Expand Up @@ -103,7 +104,7 @@ describe('translate:translate', function () {
var error = new Error('error');
var sample = getSample();
var callback = sinon.stub();
sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error);
sample.mocks.translate.getLanguages.yields(error);

sample.program.listLanguages(apiKey, callback);

Expand All @@ -114,12 +115,43 @@ describe('translate:translate', function () {
});
});

describe('listLanguageNames', function () {
it('should list languages', function () {
var sample = getSample();
var callback = sinon.stub();

sample.program.listLanguageNames(target, apiKey, callback);

assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once');
assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 2, 'method received 2 arguments');
assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result');
assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length));
});

it('should handle error', function () {
var error = new Error('error');
var sample = getSample();
var callback = sinon.stub();
sample.mocks.translate.getLanguages.yields(error);

sample.program.listLanguageNames(target, apiKey, callback);

assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
assert(callback.firstCall.args[0], 'callback received error');
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
});
});

describe('translateText', function () {
it('should translate text', function () {
var sample = getSample();
var callback = sinon.stub();
var options = {
text: text,
input: [text],
to: 'ru',
apiKey: apiKey
};
Expand All @@ -128,7 +160,7 @@ describe('translate:translate', function () {

assert(sample.mocks.translate.translate.calledOnce, 'method called once');
assert.equal(sample.mocks.translate.translate.firstCall.args.length, 3, 'method received 3 arguments');
assert.equal(sample.mocks.translate.translate.firstCall.args[0], text, 'method received correct first argument');
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[0], [text], 'method received correct first argument');
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], {
to: 'ru',
from: undefined
Expand All @@ -149,7 +181,7 @@ describe('translate:translate', function () {
to: 'ru',
apiKey: apiKey
};
sample.mocks.translate.translate = sinon.stub().callsArgWith(2, error);
sample.mocks.translate.translate.yields(error);

sample.program.translateText(options, callback);

Expand All @@ -167,7 +199,7 @@ describe('translate:translate', function () {
sinon.stub(program, 'detectLanguage');
program.main(['detect', text, '-k', apiKey]);
assert.equal(program.detectLanguage.calledOnce, true);
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [text, apiKey]);
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [[text], apiKey]);
});

it('should call listLanguages', function () {
Expand All @@ -179,14 +211,23 @@ describe('translate:translate', function () {
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [apiKey]);
});

it('should call listLanguageNames', function () {
var program = getSample().program;

sinon.stub(program, 'listLanguageNames');
program.main(['list', target, '-k', apiKey]);
assert.equal(program.listLanguageNames.calledOnce, true);
assert.deepEqual(program.listLanguageNames.firstCall.args.slice(0, -1), [target, apiKey]);
});

it('should call translateText', function () {
var program = getSample().program;

sinon.stub(program, 'translateText');
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
assert.equal(program.translateText.calledOnce, true);
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
text: text,
input: [text],
to: 'ru',
from: undefined,
apiKey: apiKey
Expand Down
68 changes: 47 additions & 21 deletions translate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,33 @@ var ISO6391 = require('iso-639-1');

// [START detect_language]
/**
* Detect the language of the provided text.
* Detect the language of the provided text or texts.
*
* @param {string} text The text for which to detect the language.
* @param {string|string[]} input The text or texts for which to detect the language.
* @param {string} apiKey Your Translate API key.
* @param {function} cb The callback function.
*/
function detectLanguage (text, apiKey, callback) {
function detectLanguage (input, apiKey, callback) {
// Instantiate a translate client
var translate = Translate({
key: apiKey
});

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate
translate.detect(text, function (err, result) {
translate.detect(input, function (err, result) {
if (err) {
return callback(err);
}

console.log(
'Detected %s (%s) with confidence %d',
ISO6391.getName(result.language),
result.language,
result.confidence
);
console.log('Detected language:', result);
return callback(null, result);
});
}
// [END detect_language]

// [START list_languages]
/**
* List all of the authenticated project's buckets.
* Get a list of support languages.
*
* @param {string} apiKey Your Translate API key.
* @param {function} cb The callback function.
Expand All @@ -81,12 +76,37 @@ function listLanguages (apiKey, callback) {
}
// [END list_languages]

// [START list_languages_target]
/**
* Get a list of supported language with language names in a target language.
*
* @param {string} apiKey Your Translate API key.
* @param {function} cb The callback function.
*/
function listLanguageNames (target, apiKey, callback) {
// Instantiate a translate client
var translate = Translate({
key: apiKey
});

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate
translate.getLanguages(target, function (err, languages) {
if (err) {
return callback(err);
}

console.log('Found %d language(s)!', languages.length);
return callback(null, languages);
});
}
// [END list_languages_target]

// [START translate_text]
/**
* Translate the provided text.
* Translate the provided text or texts.
*
* @param {object} options Configuration options.
* @param {string} options.text The text to translate.
* @param {string} options.input The text or texts to translate.
* @param {string} options.from The language of the source text.
* @param {string} options.to The language to which to translate the text.
* @param {string} options.apiKey Your Translate API key.
Expand All @@ -104,7 +124,7 @@ function translateText (options, callback) {
};

// See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate
translate.translate(options.text, config, function (err, translation) {
translate.translate(options.input, config, function (err, translation) {
if (err) {
return callback(err);
}
Expand All @@ -123,6 +143,7 @@ var utils = require('../utils');
var program = module.exports = {
detectLanguage: detectLanguage,
listLanguages: listLanguages,
listLanguageNames: listLanguageNames,
translateText: translateText,
main: function (args) {
// Run the command-line program
Expand All @@ -132,13 +153,17 @@ var program = module.exports = {

cli
.demand(1)
.command('detect <text>', 'Detect the language of the provided text', {}, function (options) {
program.detectLanguage(options.text, options.apiKey, utils.makeHandler(false));
.command('detect <input..>', 'Detect the language of the provided text', {}, function (options) {
program.detectLanguage(options.input, options.apiKey, utils.makeHandler(false));
})
.command('list', 'List available translation languages.', {}, function (options) {
program.listLanguages(options.apiKey, utils.makeHandler());
.command('list [target]', 'List available translation languages.', {}, function (options) {
if (options.target) {
program.listLanguageNames(options.target, options.apiKey, utils.makeHandler());
} else {
program.listLanguages(options.apiKey, utils.makeHandler());
}
})
.command('translate <text>', 'Translate the provided text to the target language.', {
.command('translate <input..>', 'Translate the provided text to the target language.', {
to: {
alias: 't',
demand: true,
Expand All @@ -153,7 +178,7 @@ cli
description: 'The language of the source text.'
}
}, function (options) {
program.translateText(utils.pick(options, ['text', 'to', 'from', 'apiKey']), utils.makeHandler());
program.translateText(utils.pick(options, ['input', 'to', 'from', 'apiKey']), utils.makeHandler());
})
.option('apiKey', {
alias: 'k',
Expand All @@ -165,9 +190,10 @@ cli
})
.example('node $0 detect -k your-key "Hello world!"', 'Detect the language of "Hello world!".')
.example('node $0 list -k your-key', 'List available translation languages.')
.example('node $0 list es -k your-key', 'List available translation languages with names in Spanish.')
.example('node $0 translate -k your-key --to ru "Good morning!"', 'Translate "Good morning!" to Russian, auto-detecting English.')
.example('node $0 translate -k your-key --to ru --from en "Good morning!"', 'Translate "Good morning!" to Russian from English.')
.wrap(100)
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/translate/docs');

Expand Down

0 comments on commit cf836ca

Please sign in to comment.