Skip to content

Commit 47baf19

Browse files
authored
Add Translate API samples. Fixes GoogleCloudPlatform#176. (GoogleCloudPlatform#184)
* Add Translate API samples. Fixes GoogleCloudPlatform#176. * Fix build. * Add comment. * Address comments
1 parent ffe2476 commit 47baf19

File tree

10 files changed

+516
-0
lines changed

10 files changed

+516
-0
lines changed

Diff for: .travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ cache:
7575
- speech/node_modules/
7676
- storage/node_modules/
7777
- trace/node_modules/
78+
- translate/node_modules/
7879
- vision/node_modules/
7980

8081
env:
@@ -85,6 +86,7 @@ env:
8586

8687
before_install:
8788
- openssl aes-256-cbc -K $encrypted_fda0b707c7d5_key -iv $encrypted_fda0b707c7d5_iv -in key.json.enc -out key.json -d
89+
- npm install -g npm
8890
- npm set progress=false
8991

9092
before_script:

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ on Google Cloud Platform.
3737
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
3838
* [Google Cloud Speech API (Beta)](#google-cloud-speech-api-beta)
3939
* [Google Cloud Storage](#google-cloud-storage)
40+
* [Google Translate API](#google-translate-api)
4041
* [Google Cloud Vision API](#google-cloud-vision-api)
4142
* [Stackdriver Debugger (Beta)](#stackdriver-debugger-beta)
4243
* [Stackdriver Logging (Beta)](#stackdriver-logging-beta)
@@ -321,6 +322,16 @@ View the [Cloud Storage Node.js samples][storage_samples].
321322
[storage_docs]: https://cloud.google.com/storage/docs/
322323
[storage_samples]: storage
323324

325+
### Google Translate API
326+
327+
With the [Google Translate API][translate_docs], you can dynamically translate
328+
text between thousands of language pairs.
329+
330+
View the [Translate API Node.js samples][translate_samples].
331+
332+
[translate_docs]: https://cloud.google.com/translate/docs/
333+
[translate_samples]: translate
334+
324335
### Google Cloud Vision API
325336

326337
The [Cloud Vision API][vision_docs] allows developers to easily integrate vision

Diff for: circle.yml

+1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ dependencies:
6262
- speech/node_modules/
6363
- storage/node_modules/
6464
- trace/node_modules/
65+
- translate/node_modules/
6566
- vision/node_modules/

Diff for: scripts/install

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ queue.push('pubsub');
3838
queue.push('speech');
3939
queue.push('storage');
4040
queue.push('trace');
41+
queue.push('translate');
4142
queue.push('vision');
4243

4344
/**

Diff for: scripts/uninstall

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ queue.push('pubsub');
3838
queue.push('speech');
3939
queue.push('storage');
4040
queue.push('trace');
41+
queue.push('translate');
4142
queue.push('vision');
4243

4344
/**

Diff for: translate/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Translate API Node.js Samples
4+
5+
With the [Google Translate API][translate_docs], you can dynamically translate
6+
text between thousands of language pairs.
7+
8+
[translate_docs]: https://cloud.google.com/translate/docs/
9+
10+
## Table of Contents
11+
12+
* [Setup](#setup)
13+
* [Samples](#samples)
14+
* [Translate](#translate)
15+
16+
## Setup
17+
18+
1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
19+
1. Install dependencies:
20+
21+
npm install
22+
23+
[prereq]: ../README.md#prerequisities
24+
[run]: ../README.md#how-to-run-a-sample
25+
26+
## Samples
27+
28+
### Translate
29+
30+
View the [documentation][translate_docs] or the [source code][translate_code].
31+
32+
__Usage:__ `node translate --help`
33+
34+
```
35+
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.
39+
40+
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]
44+
45+
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.
52+
53+
For more information, see https://cloud.google.com/translate/docs
54+
```
55+
56+
[translate_docs]: https://cloud.google.com/translate/docs
57+
[translate_code]: translate.js

Diff for: translate/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "nodejs-docs-samples-translate",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache Version 2.0",
6+
"author": "Google Inc.",
7+
"scripts": {
8+
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../test/_setup.js test/*.test.js",
9+
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
10+
},
11+
"dependencies": {
12+
"@google-cloud/translate": "^0.1.1",
13+
"iso-639-1": "^1.2.1",
14+
"yargs": "^5.0.0"
15+
},
16+
"devDependencies": {
17+
"mocha": "^3.0.2"
18+
}
19+
}

Diff for: translate/system-test/translate.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var program = require('../translate');
17+
var apiKey = process.env.TRANSLATE_API_KEY;
18+
var text = 'Hello world!';
19+
20+
describe('translate:translate', function () {
21+
if (!process.env.TRANSLATE_API_KEY) {
22+
process.stdout.write('Skipping Translate API tests...\n');
23+
return;
24+
}
25+
describe('detectLanguage', function () {
26+
it('should detect language', function (done) {
27+
program.detectLanguage(text, apiKey, function (err, result) {
28+
assert.ifError(err);
29+
assert(result, 'should have received a result');
30+
assert.equal(result.language, 'en', 'should have detected english');
31+
assert(console.log.calledWith('Detected %s with confidence %d', 'English', result.confidence));
32+
done();
33+
});
34+
});
35+
});
36+
37+
describe('listLanguages', function () {
38+
it('should list languages', function (done) {
39+
program.listLanguages(apiKey, function (err, languages) {
40+
assert.ifError(err);
41+
assert(Array.isArray(languages));
42+
assert(languages.length > 0);
43+
assert(console.log.calledWith('Found %d language(s)!', languages.length));
44+
done();
45+
});
46+
});
47+
});
48+
49+
describe('translateText', function () {
50+
it('should translate text', function (done) {
51+
var options = {
52+
text: text,
53+
apiKey: apiKey,
54+
to: 'ru'
55+
};
56+
var expected = 'Привет мир!';
57+
58+
program.translateText(options, function (err, translation) {
59+
assert.ifError(err);
60+
assert.equal(translation, expected);
61+
assert(console.log.calledWith('Translated text to %s', 'Russian'));
62+
done();
63+
});
64+
});
65+
});
66+
});

Diff for: translate/test/translate.test.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var proxyquire = require('proxyquire').noCallThru();
17+
var text = 'Hello world!';
18+
var apiKey = 'key';
19+
20+
function getSample () {
21+
var languagesMock = [
22+
'en',
23+
'ru'
24+
];
25+
var resultMock = {
26+
language: 'en',
27+
confidence: 0.75,
28+
input: text
29+
};
30+
var translationMock = 'Привет мир!';
31+
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)
35+
};
36+
var TranslateMock = sinon.stub().returns(translateMock);
37+
38+
return {
39+
program: proxyquire('../translate', {
40+
'@google-cloud/translate': TranslateMock,
41+
yargs: proxyquire('yargs', {})
42+
}),
43+
mocks: {
44+
Translate: TranslateMock,
45+
translate: translateMock,
46+
languages: languagesMock,
47+
result: resultMock,
48+
translation: translationMock
49+
}
50+
};
51+
}
52+
53+
describe('translate:translate', function () {
54+
describe('detectLanguage', function () {
55+
it('should detect language', function () {
56+
var sample = getSample();
57+
var callback = sinon.stub();
58+
59+
sample.program.detectLanguage(text, apiKey, callback);
60+
61+
assert(sample.mocks.translate.detect.calledOnce, 'method called once');
62+
assert.equal(sample.mocks.translate.detect.firstCall.args.length, 2, 'method received 2 arguments');
63+
assert.equal(sample.mocks.translate.detect.firstCall.args[0], text, 'method received correct argument');
64+
assert(callback.calledOnce, 'callback called once');
65+
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
66+
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
67+
assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result');
68+
assert(console.log.calledWith('Detected %s with confidence %d', 'English', sample.mocks.result.confidence));
69+
});
70+
71+
it('should handle error', function () {
72+
var error = 'error';
73+
var sample = getSample();
74+
var callback = sinon.stub();
75+
sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error);
76+
77+
sample.program.detectLanguage(text, apiKey, callback);
78+
79+
assert(callback.calledOnce, 'callback called once');
80+
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
81+
assert(callback.firstCall.args[0], 'callback received error');
82+
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
83+
});
84+
});
85+
86+
describe('listLanguages', function () {
87+
it('should list languages', function () {
88+
var sample = getSample();
89+
var callback = sinon.stub();
90+
91+
sample.program.listLanguages(apiKey, callback);
92+
93+
assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once');
94+
assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 1, 'method received 1 argument');
95+
assert(callback.calledOnce, 'callback called once');
96+
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
97+
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
98+
assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result');
99+
assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length));
100+
});
101+
102+
it('should handle error', function () {
103+
var error = 'error';
104+
var sample = getSample();
105+
var callback = sinon.stub();
106+
sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error);
107+
108+
sample.program.listLanguages(apiKey, callback);
109+
110+
assert(callback.calledOnce, 'callback called once');
111+
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
112+
assert(callback.firstCall.args[0], 'callback received error');
113+
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
114+
});
115+
});
116+
117+
describe('translateText', function () {
118+
it('should translate text', function () {
119+
var sample = getSample();
120+
var callback = sinon.stub();
121+
var options = {
122+
text: text,
123+
to: 'ru',
124+
apiKey: apiKey
125+
};
126+
127+
sample.program.translateText(options, callback);
128+
129+
assert(sample.mocks.translate.translate.calledOnce, 'method called once');
130+
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');
132+
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], {
133+
to: 'ru',
134+
from: undefined
135+
}, 'method received correct second argument');
136+
assert(callback.calledOnce, 'callback called once');
137+
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
138+
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
139+
assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result');
140+
assert(console.log.calledWith('Translated text to %s', 'Russian'));
141+
});
142+
143+
it('should handle error', function () {
144+
var error = 'error';
145+
var sample = getSample();
146+
var callback = sinon.stub();
147+
var options = {
148+
text: text,
149+
to: 'ru',
150+
apiKey: apiKey
151+
};
152+
sample.mocks.translate.translate = sinon.stub().callsArgWith(2, error);
153+
154+
sample.program.translateText(options, callback);
155+
156+
assert(callback.calledOnce, 'callback called once');
157+
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
158+
assert(callback.firstCall.args[0], 'callback received error');
159+
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
160+
});
161+
});
162+
163+
describe('main', function () {
164+
it('should call detectLanguage', function () {
165+
var program = getSample().program;
166+
167+
sinon.stub(program, 'detectLanguage');
168+
program.main(['detect', text, '-k', apiKey]);
169+
assert(program.detectLanguage.calledOnce);
170+
});
171+
172+
it('should call listLanguages', function () {
173+
var program = getSample().program;
174+
175+
sinon.stub(program, 'listLanguages');
176+
program.main(['list', '-k', apiKey]);
177+
assert(program.listLanguages.calledOnce);
178+
});
179+
180+
it('should call translateText', function () {
181+
var program = getSample().program;
182+
183+
sinon.stub(program, 'translateText');
184+
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
185+
assert(program.translateText.calledOnce);
186+
});
187+
});
188+
});

0 commit comments

Comments
 (0)