You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
+
consttranslate=Translate();
28
24
29
25
// Detects the language. "input" can be a string for detecting the language of
30
26
// a single piece of text, or an array of strings for detecting the languages
@@ -41,11 +37,7 @@ function detectLanguage (input) {
41
37
// [START translate_list_codes]
42
38
functionlistLanguages(){
43
39
// Instantiates a client
44
-
consttranslate=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
+
consttranslate=Translate();
49
41
50
42
// Lists available translation language with their names in English (the default).
51
43
returntranslate.getLanguages()
@@ -62,11 +54,7 @@ function listLanguages () {
62
54
// [START translate_list_language_names]
63
55
functionlistLanguagesWithTarget(target){
64
56
// Instantiates a client
65
-
consttranslate=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
+
consttranslate=Translate();
70
58
71
59
// Lists available translation language with their names in a target language
72
60
returntranslate.getLanguages(target)
@@ -83,11 +71,7 @@ function listLanguagesWithTarget (target) {
83
71
// [START translate_translate_text]
84
72
functiontranslateText(input,target){
85
73
// Instantiates a client
86
-
consttranslate=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
+
consttranslate=Translate();
91
75
92
76
// Translates the text into the target language. "input" can be a string for
93
77
// translating a single piece of text, or an array of strings for translating
@@ -105,38 +89,21 @@ function translateText (input, target) {
105
89
require(`yargs`)
106
90
.demand(1)
107
91
.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
-
}
111
92
detectLanguage(opts.input);
112
93
})
113
94
.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
-
}
117
95
if(opts.target){
118
96
listLanguagesWithTarget(opts.target);
119
97
}else{
120
98
listLanguages();
121
99
}
122
100
})
123
101
.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
-
}
127
102
translateText(opts.input,opts.toLang);
128
103
})
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
-
})
137
104
.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.`)
140
107
.example(`node $0 list es`,`Lists available translation languages with names in Spanish.`)
141
108
.example(`node $0 translate ru "Good morning!"`,`Translates "Good morning!" to Russian, auto-detecting the source language.`)
0 commit comments