Class v2.Translate (8.5.0)

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.

v2

Inheritance

Service > v2.Translate

Package

@google-cloud/translate

Examples

  //- 
 // 

Custom Translation API

// // 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 
 (); 
 

Constructors

(constructor)(options)

  constructor 
 ( 
 options 
 ?: 
  
 TranslateConfig 
 ); 
 

Constructs a new instance of the Translate class

Parameter
Name
Description
options

Properties

key

  key 
 ?: 
  
 string 
 ; 
 

options

  options 
 : 
  
 TranslateConfig 
 ; 
 

Methods

detect(input)

  detect 
 ( 
 input 
 : 
  
 string 
 ) 
 : 
  
 Promise 
< [ 
 DetectResult 
 , 
  
 Metadata 
 ]>; 
 
Parameter
Name
Description
input
string
Returns
Type
Description
Promise <[ DetectResult , Metadata ]>

detect(input)

  detect 
 ( 
 input 
 : 
  
 string 
 []) 
 : 
  
 Promise 
< [ 
 DetectResult 
 [], 
  
 Metadata 
 ]>; 
 
Parameter
Name
Description
input
string[]
Returns
Type
Description
Promise <[ DetectResult [], Metadata ]>

detect(input, callback)

  detect 
 ( 
 input 
 : 
  
 string 
 , 
  
 callback 
 : 
  
 DetectCallback<DetectResult> 
 ) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string
Returns
Type
Description
void

detect(input, callback)

  detect 
 ( 
 input 
 : 
  
 string 
 [], 
  
 callback 
 : 
  
 DetectCallback<DetectResult 
 []>) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string[]
callback
Returns
Type
Description
void

getLanguages(target)

  getLanguages 
 ( 
 target 
 ?: 
  
 string 
 ) 
 : 
  
 Promise 
< [ 
 LanguageResult 
 [], 
  
 Metadata 
 ]>; 
 
Parameter
Name
Description
target
string
Returns
Type
Description
Promise <[ LanguageResult [], Metadata ]>

getLanguages(target, callback)

  getLanguages 
 ( 
 target 
 : 
  
 string 
 , 
  
 callback 
 : 
  
 GetLanguagesCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
target
string
Returns
Type
Description
void

getLanguages(callback)

  getLanguages 
 ( 
 callback 
 : 
  
 GetLanguagesCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameter
Name
Description
Returns
Type
Description
void

request(reqOpts, callback)

  request 
 ( 
 reqOpts 
 : 
  
 DecorateRequestOptions 
 , 
  
 callback 
 : 
  
 BodyResponseCallback 
 ) 
 : 
  
 void 
 ; 
 

A custom request implementation. Requests to this API may optionally use an API key for an application, not a bearer token from a service account. This means it is possible to skip the makeAuthenticatedRequest portion of the typical request lifecycle, and manually authenticate the request here.

Parameters
Name
Description
reqOpts
DecorateRequestOptions

Request options that are passed to request .

callback
BodyResponseCallback

The callback function passed to request .

Returns
Type
Description
void

translate(input, options)

  translate 
 ( 
 input 
 : 
  
 string 
 , 
  
 options 
 : 
  
 TranslateRequest 
 ) 
 : 
  
 Promise 
< [ 
 string 
 , 
  
 Metadata 
 ]>; 
 
Parameters
Name
Description
input
string
Returns
Type
Description
Promise <[string, Metadata ]>

translate(input, options)

  translate 
 ( 
 input 
 : 
  
 string 
 [], 
  
 options 
 : 
  
 TranslateRequest 
 ) 
 : 
  
 Promise 
< [ 
 string 
 [], 
  
 Metadata 
 ]>; 
 
Parameters
Name
Description
input
string[]
Returns
Type
Description
Promise <[string[], Metadata ]>

translate(input, to)

  translate 
 ( 
 input 
 : 
  
 string 
 , 
  
 to 
 : 
  
 string 
 ) 
 : 
  
 Promise 
< [ 
 string 
 , 
  
 Metadata 
 ]>; 
 
Parameters
Name
Description
input
string
to
string
Returns
Type
Description
Promise <[string, Metadata ]>

translate(input, to)

  translate 
 ( 
 input 
 : 
  
 string 
 [], 
  
 to 
 : 
  
 string 
 ) 
 : 
  
 Promise 
< [ 
 string 
 [], 
  
 Metadata 
 ]>; 
 
Parameters
Name
Description
input
string[]
to
string
Returns
Type
Description
Promise <[string[], Metadata ]>

translate(input, options, callback)

  translate 
 ( 
 input 
 : 
  
 string 
 , 
  
 options 
 : 
  
 TranslateRequest 
 , 
  
 callback 
 : 
  
 TranslateCallback<string> 
 ) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string
callback
Returns
Type
Description
void

translate(input, to, callback)

  translate 
 ( 
 input 
 : 
  
 string 
 , 
  
 to 
 : 
  
 string 
 , 
  
 callback 
 : 
  
 TranslateCallback<string> 
 ) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string
to
string
callback
Returns
Type
Description
void

translate(input, options, callback)

  translate 
 ( 
 input 
 : 
  
 string 
 [], 
  
 options 
 : 
  
 TranslateRequest 
 , 
  
 callback 
 : 
  
 TranslateCallback<string 
 []>) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string[]
callback
TranslateCallback <string[]>
Returns
Type
Description
void

translate(input, to, callback)

  translate 
 ( 
 input 
 : 
  
 string 
 [], 
  
 to 
 : 
  
 string 
 , 
  
 callback 
 : 
  
 TranslateCallback<string 
 []>) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
input
string[]
to
string
callback
TranslateCallback <string[]>
Returns
Type
Description
void
Design a Mobile Site
View Site in Mobile | Classic
Share by: