Skip to content

Commit 10e6ddb

Browse files
committed
Translate API no longer needs an API key.
1 parent ce27d67 commit 10e6ddb

5 files changed

Lines changed: 18 additions & 63 deletions

File tree

translate/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,15 @@ Commands:
3939
translate <toLang> <input..> Translates the provided text or texts to the target language.
4040
4141
Options:
42-
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable. [string]
43-
--help Show help [boolean]
42+
--help Show help [boolean]
4443
4544
Examples:
46-
node translate.js detect "Hello world!" Detects the language of "Hello world!".
47-
node translate.js detect -k YOUR_API_KEY "Hello world!" Detects the language of "Hello world!" and "Goodbye",
48-
"Goodbye" supplying the API key inline.
49-
node translate.js list -k YOUR_API_KEY Lists available translation languages with names in
50-
English, supplying the API key inline.
51-
node translate.js list es Lists available translation languages with names in
52-
Spanish.
53-
node translate.js translate ru "Good morning!" Translates "Good morning!" to Russian, auto-detecting
54-
the source language.
45+
node translate.js detect "Hello world!" Detects the language of "Hello world!".
46+
node translate.js detect "Hello world!" "Goodbye" Detects the language of "Hello world!" and "Goodbye".
47+
node translate.js list Lists available translation languages with names in English.
48+
node translate.js list es Lists available translation languages with names in Spanish.
49+
node translate.js translate ru "Good morning!" Translates "Good morning!" to Russian, auto-detecting the source
50+
language.
5551
5652
For more information, see https://cloud.google.com/translate/docs
5753
```

translate/quickstart.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
// Imports the Google Cloud client library
2020
const Translate = require('@google-cloud/translate');
2121

22-
// Your Translate API key
23-
const apiKey = 'YOUR_API_KEY';
22+
// Your Google Cloud Platform project ID
23+
const projectId = 'YOUR_PROJECT_ID';
2424

2525
// Instantiates a client
2626
const translateClient = Translate({
27-
key: apiKey
27+
projectId: projectId
2828
});
2929

3030
// The text to translate

translate/system-test/quickstart.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
'use strict';
1717

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

2321
describe(`translate:quickstart`, () => {
2422
it(`should translate a string`, (done) => {

translate/system-test/translate.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ const text = `Hello world!`;
2525
const toLang = `ru`;
2626

2727
describe(`translate:translate`, () => {
28-
const translate = Translate({
29-
key: process.env.TRANSLATE_API_KEY
30-
});
31-
if (!process.env.TRANSLATE_API_KEY) {
32-
process.stdout.write(`Skipping Translate API tests...\n`);
33-
return;
34-
}
28+
const translate = Translate();
3529

3630
it(`should detect language`, () => {
3731
const output = run(`${cmd} detect "${text}"`, cwd);

translate/translate.js

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ const Translate = require('@google-cloud/translate');
2020
// [START translate_detect_language]
2121
function detectLanguage (input) {
2222
// Instantiates a client
23-
const translate = Translate({
24-
// The Translate API uses an API key for authentication. This sample looks
25-
// for the key in an environment variable.
26-
key: process.env.TRANSLATE_API_KEY
27-
});
23+
const translate = Translate();
2824

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

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

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

9276
// Translates the text into the target language. "input" can be a string for
9377
// translating a single piece of text, or an array of strings for translating
@@ -105,38 +89,21 @@ function translateText (input, target) {
10589
require(`yargs`)
10690
.demand(1)
10791
.command(`detect <input..>`, `Detects the language of the provided text or texts`, {}, (opts) => {
108-
if (!process.env.TRANSLATE_API_KEY) {
109-
process.env.TRANSLATE_API_KEY = opts.apiKey;
110-
}
11192
detectLanguage(opts.input);
11293
})
11394
.command(`list [target]`, `Lists available translation languages. To return language names in a language other than English, specify a target language.`, {}, (opts) => {
114-
if (!process.env.TRANSLATE_API_KEY) {
115-
process.env.TRANSLATE_API_KEY = opts.apiKey;
116-
}
11795
if (opts.target) {
11896
listLanguagesWithTarget(opts.target);
11997
} else {
12098
listLanguages();
12199
}
122100
})
123101
.command(`translate <toLang> <input..>`, `Translates the provided text or texts to the target language.`, {}, (opts) => {
124-
if (!process.env.TRANSLATE_API_KEY) {
125-
process.env.TRANSLATE_API_KEY = opts.apiKey;
126-
}
127102
translateText(opts.input, opts.toLang);
128103
})
129-
.option(`apiKey`, {
130-
alias: `k`,
131-
global: true,
132-
requiresArg: true,
133-
default: process.env.TRANSLATE_API_KEY,
134-
type: `string`,
135-
description: `Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.`
136-
})
137104
.example(`node $0 detect "Hello world!"`, `Detects the language of "Hello world!".`)
138-
.example(`node $0 detect -k YOUR_API_KEY "Hello world!" "Goodbye"`, `Detects the language of "Hello world!" and "Goodbye", supplying the API key inline.`)
139-
.example(`node $0 list -k YOUR_API_KEY`, `Lists available translation languages with names in English, supplying the API key inline.`)
105+
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the language of "Hello world!" and "Goodbye".`)
106+
.example(`node $0 list`, `Lists available translation languages with names in English.`)
140107
.example(`node $0 list es`, `Lists available translation languages with names in Spanish.`)
141108
.example(`node $0 translate ru "Good morning!"`, `Translates "Good morning!" to Russian, auto-detecting the source language.`)
142109
.wrap(120)

0 commit comments

Comments
 (0)