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
Package
@google-cloud/translateExamples
//-
// 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
Properties
key
key
?:
string
;
options
options
:
TranslateConfig
;
Methods
detect(input)
detect
(
input
:
string
)
:
Promise
< [
DetectResult
,
Metadata
]>;
input
string
detect(input)
detect
(
input
:
string
[])
:
Promise
< [
DetectResult
[],
Metadata
]>;
input
string[]
detect(input, callback)
detect
(
input
:
string
,
callback
:
DetectCallback<DetectResult>
)
:
void
;
void
detect(input, callback)
detect
(
input
:
string
[],
callback
:
DetectCallback<DetectResult
[]>)
:
void
;
void
getLanguages(target)
getLanguages
(
target
?:
string
)
:
Promise
< [
LanguageResult
[],
Metadata
]>;
target
string
getLanguages(target, callback)
getLanguages
(
target
:
string
,
callback
:
GetLanguagesCallback
)
:
void
;
void
getLanguages(callback)
getLanguages
(
callback
:
GetLanguagesCallback
)
:
void
;
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.
reqOpts
callback
void
translate(input, options)
translate
(
input
:
string
,
options
:
TranslateRequest
)
:
Promise
< [
string
,
Metadata
]>;
Promise
<[string, Metadata
]>
translate(input, options)
translate
(
input
:
string
[],
options
:
TranslateRequest
)
:
Promise
< [
string
[],
Metadata
]>;
Promise
<[string[], Metadata
]>
translate(input, to)
translate
(
input
:
string
,
to
:
string
)
:
Promise
< [
string
,
Metadata
]>;
input
string
to
string
Promise
<[string, Metadata
]>
translate(input, to)
translate
(
input
:
string
[],
to
:
string
)
:
Promise
< [
string
[],
Metadata
]>;
input
string[]
to
string
Promise
<[string[], Metadata
]>
translate(input, options, callback)
translate
(
input
:
string
,
options
:
TranslateRequest
,
callback
:
TranslateCallback<string>
)
:
void
;
void
translate(input, to, callback)
translate
(
input
:
string
,
to
:
string
,
callback
:
TranslateCallback<string>
)
:
void
;
void
translate(input, options, callback)
translate
(
input
:
string
[],
options
:
TranslateRequest
,
callback
:
TranslateCallback<string
[]>)
:
void
;
void
translate(input, to, callback)
translate
(
input
:
string
[],
to
:
string
,
callback
:
TranslateCallback<string
[]>)
:
void
;
void