Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Premium translation sample. #274

Merged
merged 1 commit into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
60 changes: 60 additions & 0 deletions translate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const Translate = require('@google-cloud/translate');

// [START translate_detect_language]
function detectLanguage (input) {
// The text for which to detect language, e.g.:
// input = 'Hello, world';

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

Expand Down Expand Up @@ -63,6 +66,9 @@ function listLanguages () {

// [START translate_list_language_names]
function listLanguagesWithTarget (target) {
// The target language for language names, e.g.:
// target = 'ru';

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

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

// [START translate_translate_text]
function translateText (input, target) {
// The text to translate, e.g.:
// input = 'Hello, world';
// The target language, e.g.:
// target = 'ru';

if (!Array.isArray(input)) {
input = [input];
}
Expand All @@ -107,6 +118,48 @@ function translateText (input, target) {
}
// [END translate_translate_text]

// [START translate_text_with_model]
function translateTextWithModel (input, target, model) {
// The text to translate, e.g.:
// input = 'Hello, world';
// The target language, e.g.:
// target = 'ru';
// The model to use, e.g.:
// model = 'nmt';

if (!Array.isArray(input)) {
input = [input];
}

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

const options = {
// The target language, e.g. "ru"
to: target,
// Make sure your project is whitelisted.
// Possible values are "base" and "nmt"
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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not that we should use it, but there's a library for this. 😛

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I feel that using a library for something like this is taking the Node.js "module all the things" too far...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the brevity/idiomaticity of arrify(...) vs. a ternary operator, but you're right in the sense that using it would add another dependency (= another potential point of failure).


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 +186,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