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/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
Properties
key
options
options: TranslateConfig;
Methods
detect(input)
detect(input: string): Promise<[DetectResult, Metadata]>;
Parameter |
---|
Name | Description |
input |
string
|
detect(input)
detect(input: string[]): Promise<[DetectResult[], Metadata]>;
Parameter |
---|
Name | Description |
input |
string[]
|
detect(input, callback)
detect(input: string, callback: DetectCallback<DetectResult>): void;
Returns |
---|
Type | Description |
void | |
detect(input, callback)
detect(input: string[], callback: DetectCallback<DetectResult[]>): void;
Returns |
---|
Type | Description |
void | |
getLanguages(target)
getLanguages(target?: string): Promise<[LanguageResult[], Metadata]>;
Parameter |
---|
Name | Description |
target |
string
|
getLanguages(target, callback)
getLanguages(target: string, callback: GetLanguagesCallback): void;
Returns |
---|
Type | Description |
void | |
getLanguages(callback)
getLanguages(callback: GetLanguagesCallback): void;
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.
Returns |
---|
Type | Description |
void | |
translate(input, options)
translate(input: string, options: TranslateRequest): Promise<[string, Metadata]>;
Returns |
---|
Type | Description |
Promise<[string, Metadata]> | |
translate(input, options)
translate(input: string[], options: TranslateRequest): Promise<[string[], Metadata]>;
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;
Returns |
---|
Type | Description |
void | |
translate(input, to, callback)
translate(input: string, to: string, callback: TranslateCallback<string>): void;
Returns |
---|
Type | Description |
void | |
translate(input, options, callback)
translate(input: string[], options: TranslateRequest, callback: TranslateCallback<string[]>): void;
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 | |