Skip to content

Commit fb868dc

Browse files
committed
Add Premium translation sample.
1 parent 4e70bcf commit fb868dc

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,40 @@ function translateText (input, target) {
107107
}
108108
// [END translate_translate_text]
109109

110+
// [START translate_text_with_model]
111+
function translateTextWithModel (input, target, model) {
112+
if (!Array.isArray(input)) {
113+
input = [input];
114+
}
115+
116+
// Instantiates a client
117+
const translate = Translate();
118+
119+
const options = {
120+
to: target,
121+
// Make sure your project is whitelisted. Please refer to the upstream
122+
// documentation for possible values.
123+
model: model
124+
};
125+
126+
// Translates the text into the target language. "input" can be a string for
127+
// translating a single piece of text, or an array of strings for translating
128+
// multiple texts.
129+
return translate.translate(input, options)
130+
.then((results) => {
131+
let translations = results[0];
132+
translations = Array.isArray(translations) ? translations : [translations];
133+
134+
console.log('Translations:');
135+
translations.forEach((translation, i) => {
136+
console.log(`${input[i]} => (${target}) ${translation}`);
137+
});
138+
139+
return translations;
140+
});
141+
}
142+
// [END translate_text_with_model]
143+
110144
require(`yargs`)
111145
.demand(1)
112146
.command(
@@ -133,12 +167,19 @@ require(`yargs`)
133167
{},
134168
(opts) => translateText(opts.input, opts.toLang)
135169
)
170+
.command(
171+
`translate-with-model <toLang> <model> <input..>`,
172+
`Translates one or more strings into the target language using the specified model.`,
173+
{},
174+
(opts) => translateTextWithModel(opts.input, opts.toLang, opts.model)
175+
)
136176
.example(`node $0 detect "Hello world!"`, `Detects the language of a string.`)
137177
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the languages of multiple strings.`)
138178
.example(`node $0 list`, `Lists available translation languages with names in English.`)
139179
.example(`node $0 list es`, `Lists available translation languages with names in Spanish.`)
140180
.example(`node $0 translate ru "Good morning!"`, `Translates a string into Russian.`)
141181
.example(`node $0 translate ru "Good morning!" "Good night!"`, `Translates multiple strings into Russian.`)
182+
.example(`node $0 translate-with-model ru nmt "Good morning!" "Good night!"`, `Translates multiple strings into Russian using the Premium model.`)
142183
.wrap(120)
143184
.recommendCommands()
144185
.epilogue(`For more information, see https://cloud.google.com/translate/docs`)

0 commit comments

Comments
 (0)