Constructor
new Translate(optionsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
ClientConfig |
<optional> |
Configuration options. |
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];
});
Here's a full example:
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const text = 'The text for which to detect language, e.g. Hello, world!';
// Detects the language. "text" can be a string for detecting the language of
// a single piece of text, or an array of strings for detecting the languages
// of multiple texts.
async function detectLanguage() {
let [detections] = await translate.detect(text);
detections = Array.isArray(detections) ? detections : [detections];
console.log('Detections:');
detections.forEach(detection => {
console.log(`${detection.input} => ${detection.language}`);
});
}
detectLanguage();
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> |
Examples
Gets the language names in English:
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
async function listLanguages() {
// Lists available translation language with their names in English (the default).
const [languages] = await translate.getLanguages();
console.log('Languages:');
languages.forEach(language => console.log(language));
}
listLanguages();
Gets the language names in a language other than English:
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const target = 'The target language for language names, e.g. ru';
async function listLanguagesWithTarget() {
// Lists available translation language with their names in a target language
const [languages] = await translate.getLanguages(target);
console.log('Languages:');
languages.forEach(language => console.log(language));
}
listLanguagesWithTarget();
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.
|
callback |
TranslateCallback |
<optional> |
Callback function. |
Returns:
Type | Description |
---|---|
Promise.<TranslateResponse> |
- See:
Throws:
-
If
options
is provided as an object without ato
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];
});
Full translation example:
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const text = 'The text to translate, e.g. Hello, world!';
// const target = 'The target language, e.g. ru';
async function translateText() {
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translateText();
Translation using the premium model:
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const text = 'The text to translate, e.g. Hello, world!';
// const target = 'The target language, e.g. ru';
// const model = 'The model to use, e.g. nmt';
async function translateTextWithModel() {
const options = {
// The target language, e.g. "ru"
to: target,
// Make sure your project is on the allow list.
// Possible values are "base" and "nmt"
model: model,
};
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, options);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translateTextWithModel();