Skip to content

Commit

Permalink
Translate API no longer needs an API key.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Nov 16, 2016
1 parent ce27d67 commit 10e6ddb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 63 deletions.
18 changes: 7 additions & 11 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,15 @@ Commands:
translate <toLang> <input..> Translates the provided text or texts 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]
--help Show help [boolean]
Examples:
node translate.js detect "Hello world!" Detects the language of "Hello world!".
node translate.js detect -k YOUR_API_KEY "Hello world!" Detects the language of "Hello world!" and "Goodbye",
"Goodbye" supplying the API key inline.
node translate.js list -k YOUR_API_KEY Lists available translation languages with names in
English, supplying the API key inline.
node translate.js list es Lists available translation languages with names in
Spanish.
node translate.js translate ru "Good morning!" Translates "Good morning!" to Russian, auto-detecting
the source language.
node translate.js detect "Hello world!" Detects the language of "Hello world!".
node translate.js detect "Hello world!" "Goodbye" Detects the language of "Hello world!" and "Goodbye".
node translate.js list Lists available translation languages with names in English.
node translate.js list es Lists available translation languages with names in Spanish.
node translate.js translate ru "Good morning!" Translates "Good morning!" to Russian, auto-detecting the source
language.
For more information, see https://cloud.google.com/translate/docs
```
Expand Down
6 changes: 3 additions & 3 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// Imports the Google Cloud client library
const Translate = require('@google-cloud/translate');

// Your Translate API key
const apiKey = 'YOUR_API_KEY';
// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const translateClient = Translate({
key: apiKey
projectId: projectId
});

// The text to translate
Expand Down
4 changes: 1 addition & 3 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const translate = proxyquire(`@google-cloud/translate`, {})({
key: process.env.TRANSLATE_API_KEY
});
const translate = proxyquire(`@google-cloud/translate`, {})();

describe(`translate:quickstart`, () => {
it(`should translate a string`, (done) => {
Expand Down
8 changes: 1 addition & 7 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ const text = `Hello world!`;
const toLang = `ru`;

describe(`translate:translate`, () => {
const translate = Translate({
key: process.env.TRANSLATE_API_KEY
});
if (!process.env.TRANSLATE_API_KEY) {
process.stdout.write(`Skipping Translate API tests...\n`);
return;
}
const translate = Translate();

it(`should detect language`, () => {
const output = run(`${cmd} detect "${text}"`, cwd);
Expand Down
45 changes: 6 additions & 39 deletions translate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const Translate = require('@google-cloud/translate');
// [START translate_detect_language]
function detectLanguage (input) {
// Instantiates a client
const translate = Translate({
// The Translate API uses an API key for authentication. This sample looks
// for the key in an environment variable.
key: process.env.TRANSLATE_API_KEY
});
const translate = Translate();

// Detects the language. "input" can be a string for detecting the language of
// a single piece of text, or an array of strings for detecting the languages
Expand All @@ -41,11 +37,7 @@ function detectLanguage (input) {
// [START translate_list_codes]
function listLanguages () {
// Instantiates a client
const translate = Translate({
// The Translate API uses an API key for authentication. This sample looks
// for the key in an environment variable.
key: process.env.TRANSLATE_API_KEY
});
const translate = Translate();

// Lists available translation language with their names in English (the default).
return translate.getLanguages()
Expand All @@ -62,11 +54,7 @@ function listLanguages () {
// [START translate_list_language_names]
function listLanguagesWithTarget (target) {
// Instantiates a client
const translate = Translate({
// The Translate API uses an API key for authentication. This sample looks
// for the key in an environment variable.
key: process.env.TRANSLATE_API_KEY
});
const translate = Translate();

// Lists available translation language with their names in a target language
return translate.getLanguages(target)
Expand All @@ -83,11 +71,7 @@ function listLanguagesWithTarget (target) {
// [START translate_translate_text]
function translateText (input, target) {
// Instantiates a client
const translate = Translate({
// The Translate API uses an API key for authentication. This sample looks
// for the key in an environment variable.
key: process.env.TRANSLATE_API_KEY
});
const translate = Translate();

// Translates the text into the target language. "input" can be a string for
// translating a single piece of text, or an array of strings for translating
Expand All @@ -105,38 +89,21 @@ function translateText (input, target) {
require(`yargs`)
.demand(1)
.command(`detect <input..>`, `Detects the language of the provided text or texts`, {}, (opts) => {
if (!process.env.TRANSLATE_API_KEY) {
process.env.TRANSLATE_API_KEY = opts.apiKey;
}
detectLanguage(opts.input);
})
.command(`list [target]`, `Lists available translation languages. To return language names in a language other than English, specify a target language.`, {}, (opts) => {
if (!process.env.TRANSLATE_API_KEY) {
process.env.TRANSLATE_API_KEY = opts.apiKey;
}
if (opts.target) {
listLanguagesWithTarget(opts.target);
} else {
listLanguages();
}
})
.command(`translate <toLang> <input..>`, `Translates the provided text or texts to the target language.`, {}, (opts) => {
if (!process.env.TRANSLATE_API_KEY) {
process.env.TRANSLATE_API_KEY = opts.apiKey;
}
translateText(opts.input, opts.toLang);
})
.option(`apiKey`, {
alias: `k`,
global: true,
requiresArg: true,
default: process.env.TRANSLATE_API_KEY,
type: `string`,
description: `Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.`
})
.example(`node $0 detect "Hello world!"`, `Detects the language of "Hello world!".`)
.example(`node $0 detect -k YOUR_API_KEY "Hello world!" "Goodbye"`, `Detects the language of "Hello world!" and "Goodbye", supplying the API key inline.`)
.example(`node $0 list -k YOUR_API_KEY`, `Lists available translation languages with names in English, supplying the API key inline.`)
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the language of "Hello world!" and "Goodbye".`)
.example(`node $0 list`, `Lists available translation languages with names in English.`)
.example(`node $0 list es`, `Lists available translation languages with names in Spanish.`)
.example(`node $0 translate ru "Good morning!"`, `Translates "Good morning!" to Russian, auto-detecting the source language.`)
.wrap(120)
Expand Down

0 comments on commit 10e6ddb

Please sign in to comment.