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 Sep 1, 2016
1 parent 241d28f commit f840a08
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 194 deletions.
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
56 changes: 39 additions & 17 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,74 @@

'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);
program.detectLanguage(text, function (err, result) {
assert.equal(err, null);
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.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]);
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) {
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]);
done();
});
});
});

describe('listLanguagesWithTarget', function () {
it('should list languages with a target', function (done) {
program.listLanguagesWithTarget('es', function (err, languages) {
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]);
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) {
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)]);
done();
});
});
Expand Down
Loading

0 comments on commit f840a08

Please sign in to comment.