Translate

Translate

With Google Translate, you can dynamically translate text between thousands of language pairs.

The Google Cloud Translation API lets websites and programs integrate with Google Translate programmatically.

Constructor

new Translate(optionsopt)

Parameters:
Name Type Attributes Description
options ClientConfig <optional>

Configuration options.

See:
Examples
```
//-
// <h3>Custom Translation API</h3>
//
// The environment variable, `GOOGLE_CLOUD_TRANSLATE_ENDPOINT`, is honored as
// a custom backend which our library will send requests to.
//-

```

Full quickstart example:

  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const projectId = 'YOUR_PROJECT_ID';

  // Imports the Google Cloud client library
  const {Translate} = require('@google-cloud/translate').v2;

  // Instantiates a client
  const translate = new Translate({projectId});

  async function quickStart() {
    // The text to translate
    const text = 'Hello, world!';

    // The target language
    const target = 'ru';

    // Translates some text into Russian
    const [translation] = await translate.translate(text, target);
    console.log(`Text: ${text}`);
    console.log(`Translation: ${translation}`);
  }

  quickStart();

Methods

detect(input, callbackopt) → {Promise.<DetectResponse>}

Detect the language used in a string or multiple strings.

Parameters:
Name Type Attributes Description
input string | Array.<string>

The source string input.

callback DetectCallback <optional>

Callback function.

Returns:
Type Description
Promise.<DetectResponse>
See:
Examples
```
const {Translate} = require('@google-cloud/translate');

const translate = new Translate();

//-
// Detect the language from a single string input.
//-
translate.detect('Hello', (err, results) => {
  if (!err) {
    // results = {
    //   language: 'en',
    //   confidence: 1,
    //   input: 'Hello'
    // }
  }
});

//-
// Detect the languages used in multiple strings. Note that the results are
// now provided as an array.
//-
translate.detect([
  'Hello',
  'Hola'
], (err, results) => {
  if (!err) {
    // results = [
    //   {
    //     language: 'en',
    //     confidence: 1,
    //     input: 'Hello'
    //   },
    //   {
    //     language: 'es',
    //     confidence: 1,
    //     input: 'Hola'
    //   }
    // ]
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
translate.detect('Hello').then((data) => {
  const results = data[0];
  const apiResponse = data[2];
});

```

include:samples/translate.js

region_tag:translate_detect_language
Here's a full example:

getLanguages(targetopt, callbackopt) → {Promise.<GetLanguagesResponse>}

Get an array of all supported languages.

Parameters:
Name Type Attributes Description
target string <optional>

Get the language names in a language other than English.

callback GetLanguagesCallback <optional>

Callback function.

Returns:
Type Description
Promise.<GetLanguagesResponse>
See:
Examples

include:samples/translate.js

region_tag:translate_list_codes
Gets the language names in English:

include:samples/translate.js

region_tag:translate_list_language_names
Gets the language names in a language other than English:

translate(input, optionsopt, callbackopt) → {Promise.<TranslateResponse>}

Translate a string or multiple strings into another language.

Parameters:
Name Type Attributes Description
input string | Array.<string>

The source string input.

options string | TranslateRequest <optional>

If a string, it is interpreted as the target ISO 639-1 language code to translate the source input to. (e.g. en for English). If an object, you may also specify the source language.

callback TranslateCallback <optional>

Callback function.

Returns:
Type Description
Promise.<TranslateResponse>
See:
Throws:

If options is provided as an object without a to property.

Type
Error
Examples
```
//-
// Pass a string and a language code to get the translation.
//-
translate.translate('Hello', 'es', (err, translation) => {
  if (!err) {
    // translation = 'Hola'
  }
});

//-
// The source language is auto-detected by default. To manually set it,
// provide an object.
//-
const options = {
  from: 'en',
  to: 'es'
};

translate.translate('Hello', options, (err, translation) => {
  if (!err) {
    // translation = 'Hola'
  }
});

//-
// Translate multiple strings of input. Note that the results are
// now provided as an array.
//-
const input = [
  'Hello',
  'How are you today?'
];

translate.translate(input, 'es', (err, translations) => {
  if (!err) {
    // translations = [
    //   'Hola',
    //   'Como estas hoy?'
    // ]
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
translate.translate('Hello', 'es').then((data) => {
  const translation = data[0];
  const apiResponse = data[1];
});

```

include:samples/translate.js

region_tag:translate_translate_text
Full translation example:

include:samples/translate.js

region_tag:translate_text_with_model
Translation using the premium model: