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

Add remaining Translate samples. #197

Merged
merged 1 commit into from
Sep 1, 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
30 changes: 18 additions & 12 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,28 @@ __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 or texts
list [target] List available translation languages. To return language names in a language other than
English, specify a target language.
translate <toLang> <input..> Translate the provided text or texts to the target language, optionally specifying the
source 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]
--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 "Hello world!" Detect the language of "Hello world!".
node translate detect -k your-api-key "Hello world!" Detect the language of "Hello world!" and "Goodbye",
"Goodbye" supplying the API key inline..
node translate list -k your-api-key List available translation languages with names in
English, supplying the API key inline..
node translate list es List available translation languages with names in
Spanish.
node translate translate ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting the
source language.
node translate translate ru "Good morning!" -f en -k Translate "Good morning!" to Russian from English,
your-api-key supplying the API key inline.
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
62 changes: 44 additions & 18 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,78 @@

'use strict';

var ISO6391 = require('iso-639-1');
var program = require('../translate');
var apiKey = process.env.TRANSLATE_API_KEY;

var text = 'Hello world!';
var toLang = 'ru';

describe('translate:translate', function () {
if (!process.env.TRANSLATE_API_KEY) {
process.stdout.write('Skipping Translate API tests...\n');
return;
}

describe('detectLanguage', function () {
it('should detect language', function (done) {
program.detectLanguage(text, apiKey, function (err, result) {
assert.ifError(err);
assert(result, 'should have received a result');
program.detectLanguage(text, function (err, result, apiResponse) {
assert.equal(err, null);
assert.notEqual(result, undefined);
assert.equal(result.language, 'en', 'should have detected english');
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]);
assert.notEqual(apiResponse, undefined);
done();
});
});
});

describe('listLanguages', function () {
it('should list languages', function (done) {
program.listLanguages(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));
program.listLanguages(function (err, languages, apiResponse) {
assert.equal(err, null);
assert.equal(Array.isArray(languages), true);
assert.equal(languages.length > 0, true);
var matchingLanguages = languages.filter(function (language) {
return language.code === 'af' && language.name === 'Afrikaans';
});
assert.equal(matchingLanguages.length, 1, 'found language with name in English');
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
assert.notEqual(apiResponse, undefined);
done();
});
});
});

describe('listLanguagesWithTarget', function () {
it('should list languages with a target', function (done) {
program.listLanguagesWithTarget('es', function (err, languages, apiResponse) {
assert.equal(err, null);
assert.equal(Array.isArray(languages), true);
assert.equal(languages.length > 0, true);
var matchingLanguages = languages.filter(function (language) {
return language.code === 'af' && language.name === 'afrikáans';
});
assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish');
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]);
assert.notEqual(apiResponse, undefined);
done();
});
});
});

describe('translateText', function () {
it('should translate text', function (done) {
var options = {
text: text,
apiKey: apiKey,
to: 'ru'
};
var expected = 'Привет мир!';

program.translateText(options, function (err, translation) {
assert.ifError(err);
program.translateText(text, toLang, undefined, function (err, translation, apiResponse) {
assert.equal(err, null);
assert.equal(translation, expected);
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]);
assert.notEqual(apiResponse, undefined);
done();
});
});
Expand Down
Loading