v2. Translate
Source: v2/
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.
new Translate([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.
//-
include:samples/quickstart.js
region_tag:translate_quickstart
Full quickstart example:
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
options |
|
Yes |
Configuration options. |
Methods
detect(input[, callback]) → Promise containing DetectResponse
Detect the language used in a string or multiple strings.
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:
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
input |
(string or Array of string) |
|
The source string input. |
|
callback |
DetectCallback |
Yes |
Callback function. |
- See also
- Returns
-
Promise containing DetectResponse
getLanguages([target][, callback]) → Promise containing GetLanguagesResponse
Get an array of all supported languages.
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:
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
target |
string |
Yes |
Get the language names in a language other than English. |
|
callback |
GetLanguagesCallback |
Yes |
Callback function. |
- See also
- Returns
-
Promise containing GetLanguagesResponse
translate(input[, options][, callback]) → Promise containing TranslateResponse
Translate a string or multiple strings into another language.
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:
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
input |
(string or Array of string) |
|
The source string input. |
|
options |
(string or TranslateRequest) |
Yes |
If a string, it is interpreted as the
target ISO 639-1 language code to translate the source input to. (e.g.
|
|
callback |
TranslateCallback |
Yes |
Callback function. |
- See also
- Throws
-
ErrorIf
optionsis provided as an object without atoproperty. - Returns
-
Promise containing TranslateResponse