Skip to content

Commit cf836ca

Browse files
committed
Add remaining Translate samples.
1 parent 241d28f commit cf836ca

File tree

5 files changed

+128
-47
lines changed

5 files changed

+128
-47
lines changed

translate/README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,24 @@ __Usage:__ `node translate --help`
3333

3434
```
3535
Commands:
36-
detect <text> Detect the language of the provided text
37-
list List available translation languages.
38-
translate <text> Translate the provided text to the target language.
36+
detect <input..> Detect the language of the provided text
37+
list [target] List available translation languages.
38+
translate <input..> Translate the provided text to the target language.
3939
4040
Options:
41-
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment
42-
variable. [string]
43-
--help Show help [boolean]
41+
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.
42+
[string] [default: "AIzaSyCxFhP0edrbIJs41_TeMSHH3V2dzWeC_NY"]
43+
--help Show help [boolean]
4444
4545
Examples:
46-
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
47-
node translate list -k your-key List available translation languages.
48-
node translate translate -k your-key --to ru "Good Translate "Good morning!" to Russian,
49-
morning!" auto-detecting English.
50-
node translate translate -k your-key --to ru Translate "Good morning!" to Russian from
51-
--from en "Good morning!" English.
46+
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
47+
node translate list -k your-key List available translation languages.
48+
node translate list es -k your-key List available translation languages with names in
49+
Spanish.
50+
node translate translate -k your-key --to ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting
51+
English.
52+
node translate translate -k your-key --to ru --from en "Good Translate "Good morning!" to Russian from English.
53+
morning!"
5254
5355
For more information, see https://cloud.google.com/translate/docs
5456
```

translate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
1010
},
1111
"dependencies": {
12-
"@google-cloud/translate": "^0.1.1",
12+
"@google-cloud/translate": "^0.2.0",
1313
"iso-639-1": "^1.2.1",
1414
"yargs": "^5.0.0"
1515
},

translate/system-test/translate.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('translate:translate', function () {
2828
assert.ifError(err);
2929
assert(result, 'should have received a result');
3030
assert.equal(result.language, 'en', 'should have detected english');
31-
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
31+
assert(console.log.calledWith('Detected language:', result));
3232
done();
3333
});
3434
});
@@ -46,10 +46,22 @@ describe('translate:translate', function () {
4646
});
4747
});
4848

49+
describe('listLanguageNames', function () {
50+
it('should list languages with names in target language', function (done) {
51+
program.listLanguageNames('es', apiKey, function (err, languages) {
52+
assert.ifError(err);
53+
assert(Array.isArray(languages));
54+
assert(languages.length > 0);
55+
assert(console.log.calledWith('Found %d language(s)!', languages.length));
56+
done();
57+
});
58+
});
59+
});
60+
4961
describe('translateText', function () {
5062
it('should translate text', function (done) {
5163
var options = {
52-
text: text,
64+
input: text,
5365
apiKey: apiKey,
5466
to: 'ru'
5567
};

translate/test/translate.test.js

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var proxyquire = require('proxyquire').noCallThru();
1717
var text = 'Hello world!';
1818
var apiKey = 'key';
19+
var target = 'es';
1920

2021
function getSample () {
2122
var languagesMock = [
@@ -29,9 +30,9 @@ function getSample () {
2930
};
3031
var translationMock = 'Привет мир!';
3132
var translateMock = {
32-
getLanguages: sinon.stub().callsArgWith(0, null, languagesMock),
33-
detect: sinon.stub().callsArgWith(1, null, resultMock),
34-
translate: sinon.stub().callsArgWith(2, null, translationMock)
33+
getLanguages: sinon.stub().yields(null, languagesMock),
34+
detect: sinon.stub().yields(null, resultMock),
35+
translate: sinon.stub().yields(null, translationMock)
3536
};
3637
var TranslateMock = sinon.stub().returns(translateMock);
3738

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

7172
it('should handle error', function () {
7273
var error = new Error('error');
7374
var sample = getSample();
7475
var callback = sinon.stub();
75-
sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error);
76+
sample.mocks.translate.detect.yields(error);
7677

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

@@ -103,7 +104,7 @@ describe('translate:translate', function () {
103104
var error = new Error('error');
104105
var sample = getSample();
105106
var callback = sinon.stub();
106-
sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error);
107+
sample.mocks.translate.getLanguages.yields(error);
107108

108109
sample.program.listLanguages(apiKey, callback);
109110

@@ -114,12 +115,43 @@ describe('translate:translate', function () {
114115
});
115116
});
116117

118+
describe('listLanguageNames', function () {
119+
it('should list languages', function () {
120+
var sample = getSample();
121+
var callback = sinon.stub();
122+
123+
sample.program.listLanguageNames(target, apiKey, callback);
124+
125+
assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once');
126+
assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 2, 'method received 2 arguments');
127+
assert(callback.calledOnce, 'callback called once');
128+
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
129+
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
130+
assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result');
131+
assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length));
132+
});
133+
134+
it('should handle error', function () {
135+
var error = new Error('error');
136+
var sample = getSample();
137+
var callback = sinon.stub();
138+
sample.mocks.translate.getLanguages.yields(error);
139+
140+
sample.program.listLanguageNames(target, apiKey, callback);
141+
142+
assert(callback.calledOnce, 'callback called once');
143+
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
144+
assert(callback.firstCall.args[0], 'callback received error');
145+
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
146+
});
147+
});
148+
117149
describe('translateText', function () {
118150
it('should translate text', function () {
119151
var sample = getSample();
120152
var callback = sinon.stub();
121153
var options = {
122-
text: text,
154+
input: [text],
123155
to: 'ru',
124156
apiKey: apiKey
125157
};
@@ -128,7 +160,7 @@ describe('translate:translate', function () {
128160

129161
assert(sample.mocks.translate.translate.calledOnce, 'method called once');
130162
assert.equal(sample.mocks.translate.translate.firstCall.args.length, 3, 'method received 3 arguments');
131-
assert.equal(sample.mocks.translate.translate.firstCall.args[0], text, 'method received correct first argument');
163+
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[0], [text], 'method received correct first argument');
132164
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], {
133165
to: 'ru',
134166
from: undefined
@@ -149,7 +181,7 @@ describe('translate:translate', function () {
149181
to: 'ru',
150182
apiKey: apiKey
151183
};
152-
sample.mocks.translate.translate = sinon.stub().callsArgWith(2, error);
184+
sample.mocks.translate.translate.yields(error);
153185

154186
sample.program.translateText(options, callback);
155187

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

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

214+
it('should call listLanguageNames', function () {
215+
var program = getSample().program;
216+
217+
sinon.stub(program, 'listLanguageNames');
218+
program.main(['list', target, '-k', apiKey]);
219+
assert.equal(program.listLanguageNames.calledOnce, true);
220+
assert.deepEqual(program.listLanguageNames.firstCall.args.slice(0, -1), [target, apiKey]);
221+
});
222+
182223
it('should call translateText', function () {
183224
var program = getSample().program;
184225

185226
sinon.stub(program, 'translateText');
186227
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
187228
assert.equal(program.translateText.calledOnce, true);
188229
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
189-
text: text,
230+
input: [text],
190231
to: 'ru',
191232
from: undefined,
192233
apiKey: apiKey

translate/translate.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,33 @@ var ISO6391 = require('iso-639-1');
2727

2828
// [START detect_language]
2929
/**
30-
* Detect the language of the provided text.
30+
* Detect the language of the provided text or texts.
3131
*
32-
* @param {string} text The text for which to detect the language.
32+
* @param {string|string[]} input The text or texts for which to detect the language.
3333
* @param {string} apiKey Your Translate API key.
3434
* @param {function} cb The callback function.
3535
*/
36-
function detectLanguage (text, apiKey, callback) {
36+
function detectLanguage (input, apiKey, callback) {
3737
// Instantiate a translate client
3838
var translate = Translate({
3939
key: apiKey
4040
});
4141

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

48-
console.log(
49-
'Detected %s (%s) with confidence %d',
50-
ISO6391.getName(result.language),
51-
result.language,
52-
result.confidence
53-
);
48+
console.log('Detected language:', result);
5449
return callback(null, result);
5550
});
5651
}
5752
// [END detect_language]
5853

5954
// [START list_languages]
6055
/**
61-
* List all of the authenticated project's buckets.
56+
* Get a list of support languages.
6257
*
6358
* @param {string} apiKey Your Translate API key.
6459
* @param {function} cb The callback function.
@@ -81,12 +76,37 @@ function listLanguages (apiKey, callback) {
8176
}
8277
// [END list_languages]
8378

79+
// [START list_languages_target]
80+
/**
81+
* Get a list of supported language with language names in a target language.
82+
*
83+
* @param {string} apiKey Your Translate API key.
84+
* @param {function} cb The callback function.
85+
*/
86+
function listLanguageNames (target, apiKey, callback) {
87+
// Instantiate a translate client
88+
var translate = Translate({
89+
key: apiKey
90+
});
91+
92+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate
93+
translate.getLanguages(target, function (err, languages) {
94+
if (err) {
95+
return callback(err);
96+
}
97+
98+
console.log('Found %d language(s)!', languages.length);
99+
return callback(null, languages);
100+
});
101+
}
102+
// [END list_languages_target]
103+
84104
// [START translate_text]
85105
/**
86-
* Translate the provided text.
106+
* Translate the provided text or texts.
87107
*
88108
* @param {object} options Configuration options.
89-
* @param {string} options.text The text to translate.
109+
* @param {string} options.input The text or texts to translate.
90110
* @param {string} options.from The language of the source text.
91111
* @param {string} options.to The language to which to translate the text.
92112
* @param {string} options.apiKey Your Translate API key.
@@ -104,7 +124,7 @@ function translateText (options, callback) {
104124
};
105125

106126
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate
107-
translate.translate(options.text, config, function (err, translation) {
127+
translate.translate(options.input, config, function (err, translation) {
108128
if (err) {
109129
return callback(err);
110130
}
@@ -123,6 +143,7 @@ var utils = require('../utils');
123143
var program = module.exports = {
124144
detectLanguage: detectLanguage,
125145
listLanguages: listLanguages,
146+
listLanguageNames: listLanguageNames,
126147
translateText: translateText,
127148
main: function (args) {
128149
// Run the command-line program
@@ -132,13 +153,17 @@ var program = module.exports = {
132153

133154
cli
134155
.demand(1)
135-
.command('detect <text>', 'Detect the language of the provided text', {}, function (options) {
136-
program.detectLanguage(options.text, options.apiKey, utils.makeHandler(false));
156+
.command('detect <input..>', 'Detect the language of the provided text', {}, function (options) {
157+
program.detectLanguage(options.input, options.apiKey, utils.makeHandler(false));
137158
})
138-
.command('list', 'List available translation languages.', {}, function (options) {
139-
program.listLanguages(options.apiKey, utils.makeHandler());
159+
.command('list [target]', 'List available translation languages.', {}, function (options) {
160+
if (options.target) {
161+
program.listLanguageNames(options.target, options.apiKey, utils.makeHandler());
162+
} else {
163+
program.listLanguages(options.apiKey, utils.makeHandler());
164+
}
140165
})
141-
.command('translate <text>', 'Translate the provided text to the target language.', {
166+
.command('translate <input..>', 'Translate the provided text to the target language.', {
142167
to: {
143168
alias: 't',
144169
demand: true,
@@ -153,7 +178,7 @@ cli
153178
description: 'The language of the source text.'
154179
}
155180
}, function (options) {
156-
program.translateText(utils.pick(options, ['text', 'to', 'from', 'apiKey']), utils.makeHandler());
181+
program.translateText(utils.pick(options, ['input', 'to', 'from', 'apiKey']), utils.makeHandler());
157182
})
158183
.option('apiKey', {
159184
alias: 'k',
@@ -165,9 +190,10 @@ cli
165190
})
166191
.example('node $0 detect -k your-key "Hello world!"', 'Detect the language of "Hello world!".')
167192
.example('node $0 list -k your-key', 'List available translation languages.')
193+
.example('node $0 list es -k your-key', 'List available translation languages with names in Spanish.')
168194
.example('node $0 translate -k your-key --to ru "Good morning!"', 'Translate "Good morning!" to Russian, auto-detecting English.')
169195
.example('node $0 translate -k your-key --to ru --from en "Good morning!"', 'Translate "Good morning!" to Russian from English.')
170-
.wrap(100)
196+
.wrap(120)
171197
.recommendCommands()
172198
.epilogue('For more information, see https://cloud.google.com/translate/docs');
173199

0 commit comments

Comments
 (0)