Skip to content

Commit

Permalink
Add Premium translation sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Dec 14, 2016
1 parent 4e70bcf commit fb868dc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
16 changes: 9 additions & 7 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ __Usage:__ `node translate.js --help`

```
Commands:
detect <input..> Detects the language of one or more strings.
list [target] Lists available translation languages. To return
language names in a language other thanEnglish,
specify a target language.
translate <toLang> <input..> Translates one or more strings into the target
language.
detect <input..> Detects the language of one or more strings.
list [target] Lists available translation languages. To return language names in a
language other than English, specify a target language.
translate <toLang> <input..> Translates one or more strings into the target language.
translate-with-model <toLang> <model> <input..> Translates one or more strings into the target language using the
specified model.
Options:
--help Show help [boolean]
--help Show help [boolean]
Examples:
node translate.js detect "Hello world!" Detects the language of a string.
Expand All @@ -52,6 +52,8 @@ Examples:
Spanish.
node translate.js translate ru "Good morning!" Translates a string into Russian.
node translate.js translate ru "Good morning!" "Good night!" Translates multiple strings into Russian.
node translate.js translate-with-model ru nmt "Good Translates multiple strings into Russian using the
morning!" "Good night!" Premium model.
For more information, see https://cloud.google.com/translate/docs
```
Expand Down
19 changes: 19 additions & 0 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
const text = `Hello world!`;
const text2 = `Goodbye!`;
const model = `nmt`;
const toLang = `ru`;

describe(`translate:translate`, () => {
Expand Down Expand Up @@ -75,4 +76,22 @@ describe(`translate:translate`, () => {
assert.equal(output, expected);
});
});

it(`should translate a single string with a model`, () => {
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
return translate.translate(text, toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
assert.equal(output, expected);
});
});

it(`should translate multiple strings with a model`, () => {
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
return translate.translate([text, text2], toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
assert.equal(output, expected);
});
});
});
41 changes: 41 additions & 0 deletions translate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ function translateText (input, target) {
}
// [END translate_translate_text]

// [START translate_text_with_model]
function translateTextWithModel (input, target, model) {
if (!Array.isArray(input)) {
input = [input];
}

// Instantiates a client
const translate = Translate();

const options = {
to: target,
// Make sure your project is whitelisted. Please refer to the upstream
// documentation for possible values.
model: model
};

// 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
// multiple texts.
return translate.translate(input, options)
.then((results) => {
let translations = results[0];
translations = Array.isArray(translations) ? translations : [translations];

console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${input[i]} => (${target}) ${translation}`);
});

return translations;
});
}
// [END translate_text_with_model]

require(`yargs`)
.demand(1)
.command(
Expand All @@ -133,12 +167,19 @@ require(`yargs`)
{},
(opts) => translateText(opts.input, opts.toLang)
)
.command(
`translate-with-model <toLang> <model> <input..>`,
`Translates one or more strings into the target language using the specified model.`,
{},
(opts) => translateTextWithModel(opts.input, opts.toLang, opts.model)
)
.example(`node $0 detect "Hello world!"`, `Detects the language of a string.`)
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the languages of multiple strings.`)
.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 a string into Russian.`)
.example(`node $0 translate ru "Good morning!" "Good night!"`, `Translates multiple strings into Russian.`)
.example(`node $0 translate-with-model ru nmt "Good morning!" "Good night!"`, `Translates multiple strings into Russian using the Premium model.`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/translate/docs`)
Expand Down

0 comments on commit fb868dc

Please sign in to comment.