Skip to content

Commit 5f367da

Browse files
committed
Add Premium translation sample
1 parent 4e70bcf commit 5f367da

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

translate/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ __Usage:__ `node translate.js --help`
3333

3434
```
3535
Commands:
36-
detect <input..> Detects the language of one or more strings.
37-
list [target] Lists available translation languages. To return
38-
language names in a language other thanEnglish,
39-
specify a target language.
40-
translate <toLang> <input..> Translates one or more strings into the target
41-
language.
36+
detect <input..> Detects the language of one or more strings.
37+
list [target] Lists available translation languages. To return language names in a
38+
language other than English, specify a target language.
39+
translate <toLang> <input..> Translates one or more strings into the target language.
40+
translate-with-model <toLang> <model> <input..> Translates one or more strings into the target language using the
41+
specified model.
4242
4343
Options:
44-
--help Show help [boolean]
44+
--help Show help [boolean]
4545
4646
Examples:
4747
node translate.js detect "Hello world!" Detects the language of a string.
@@ -52,6 +52,8 @@ Examples:
5252
Spanish.
5353
node translate.js translate ru "Good morning!" Translates a string into Russian.
5454
node translate.js translate ru "Good morning!" "Good night!" Translates multiple strings into Russian.
55+
node translate.js translate-with-model ru nmt "Good Translates multiple strings into Russian using the
56+
morning!" "Good night!" Premium model.
5557
5658
For more information, see https://cloud.google.com/translate/docs
5759
```

translate/system-test/translate.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const cwd = path.join(__dirname, `..`);
2323
const cmd = `node translate.js`;
2424
const text = `Hello world!`;
2525
const text2 = `Goodbye!`;
26+
const model = `nmt`;
2627
const toLang = `ru`;
2728

2829
describe(`translate:translate`, () => {
@@ -75,4 +76,22 @@ describe(`translate:translate`, () => {
7576
assert.equal(output, expected);
7677
});
7778
});
79+
80+
it(`should translate a single string with a model`, () => {
81+
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
82+
return translate.translate(text, toLang)
83+
.then((results) => {
84+
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
85+
assert.equal(output, expected);
86+
});
87+
});
88+
89+
it(`should translate multiple strings with a model`, () => {
90+
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
91+
return translate.translate([text, text2], toLang)
92+
.then((results) => {
93+
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
94+
assert.equal(output, expected);
95+
});
96+
});
7897
});

translate/translate.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const Translate = require('@google-cloud/translate');
1919

2020
// [START translate_detect_language]
2121
function detectLanguage (input) {
22+
// The text for which to detect language, e.g.:
23+
// input = 'Hello, world';
24+
2225
// Instantiates a client
2326
const translate = Translate();
2427

@@ -63,6 +66,9 @@ function listLanguages () {
6366

6467
// [START translate_list_language_names]
6568
function listLanguagesWithTarget (target) {
69+
// The target language for language names, e.g.:
70+
// target = 'ru';
71+
6672
// Instantiates a client
6773
const translate = Translate();
6874

@@ -82,6 +88,11 @@ function listLanguagesWithTarget (target) {
8288

8389
// [START translate_translate_text]
8490
function translateText (input, target) {
91+
// The text to translate, e.g.:
92+
// input = 'Hello, world';
93+
// The target language, e.g.:
94+
// target = 'ru';
95+
8596
if (!Array.isArray(input)) {
8697
input = [input];
8798
}
@@ -107,6 +118,48 @@ function translateText (input, target) {
107118
}
108119
// [END translate_translate_text]
109120

121+
// [START translate_text_with_model]
122+
function translateTextWithModel (input, target, model) {
123+
// The text to translate, e.g.:
124+
// input = 'Hello, world';
125+
// The target language, e.g.:
126+
// target = 'ru';
127+
// The model to use, e.g.:
128+
// model = 'nmt';
129+
130+
if (!Array.isArray(input)) {
131+
input = [input];
132+
}
133+
134+
// Instantiates a client
135+
const translate = Translate();
136+
137+
const options = {
138+
// The target language, e.g. "ru"
139+
to: target,
140+
// Make sure your project is whitelisted.
141+
// Possible values are "base" and "nmt"
142+
model: model
143+
};
144+
145+
// Translates the text into the target language. "input" can be a string for
146+
// translating a single piece of text, or an array of strings for translating
147+
// multiple texts.
148+
return translate.translate(input, options)
149+
.then((results) => {
150+
let translations = results[0];
151+
translations = Array.isArray(translations) ? translations : [translations];
152+
153+
console.log('Translations:');
154+
translations.forEach((translation, i) => {
155+
console.log(`${input[i]} => (${target}) ${translation}`);
156+
});
157+
158+
return translations;
159+
});
160+
}
161+
// [END translate_text_with_model]
162+
110163
require(`yargs`)
111164
.demand(1)
112165
.command(
@@ -133,12 +186,19 @@ require(`yargs`)
133186
{},
134187
(opts) => translateText(opts.input, opts.toLang)
135188
)
189+
.command(
190+
`translate-with-model <toLang> <model> <input..>`,
191+
`Translates one or more strings into the target language using the specified model.`,
192+
{},
193+
(opts) => translateTextWithModel(opts.input, opts.toLang, opts.model)
194+
)
136195
.example(`node $0 detect "Hello world!"`, `Detects the language of a string.`)
137196
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the languages of multiple strings.`)
138197
.example(`node $0 list`, `Lists available translation languages with names in English.`)
139198
.example(`node $0 list es`, `Lists available translation languages with names in Spanish.`)
140199
.example(`node $0 translate ru "Good morning!"`, `Translates a string into Russian.`)
141200
.example(`node $0 translate ru "Good morning!" "Good night!"`, `Translates multiple strings into Russian.`)
201+
.example(`node $0 translate-with-model ru nmt "Good morning!" "Good night!"`, `Translates multiple strings into Russian using the Premium model.`)
142202
.wrap(120)
143203
.recommendCommands()
144204
.epilogue(`For more information, see https://cloud.google.com/translate/docs`)

0 commit comments

Comments
 (0)