Class v2.Translate (8.0.3)

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
NameDescription
options TranslateConfig

Properties

key

key?: string;

options

options: TranslateConfig;

Methods

detect(input)

detect(input: string): Promise<[DetectResult, Metadata]>;
Parameter
NameDescription
input string
Returns
TypeDescription
Promise<[DetectResult, Metadata]>

detect(input)

detect(input: string[]): Promise<[DetectResult[], Metadata]>;
Parameter
NameDescription
input string[]
Returns
TypeDescription
Promise<[DetectResult[], Metadata]>

detect(input, callback)

detect(input: string, callback: DetectCallback<DetectResult>): void;
Parameters
NameDescription
input string
callback DetectCallback<DetectResult>
Returns
TypeDescription
void

detect(input, callback)

detect(input: string[], callback: DetectCallback<DetectResult[]>): void;
Parameters
NameDescription
input string[]
callback DetectCallback<DetectResult[]>
Returns
TypeDescription
void

getLanguages(target)

getLanguages(target?: string): Promise<[LanguageResult[], Metadata]>;
Parameter
NameDescription
target string
Returns
TypeDescription
Promise<[LanguageResult[], Metadata]>

getLanguages(target, callback)

getLanguages(target: string, callback: GetLanguagesCallback): void;
Parameters
NameDescription
target string
callback GetLanguagesCallback
Returns
TypeDescription
void

getLanguages(callback)

getLanguages(callback: GetLanguagesCallback): void;
Parameter
NameDescription
callback GetLanguagesCallback
Returns
TypeDescription
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
NameDescription
reqOpts DecorateRequestOptions

Request options that are passed to request.

callback BodyResponseCallback

The callback function passed to request.

Returns
TypeDescription
void

translate(input, options)

translate(input: string, options: TranslateRequest): Promise<[string, Metadata]>;
Parameters
NameDescription
input string
options TranslateRequest
Returns
TypeDescription
Promise<[string, Metadata]>

translate(input, options)

translate(input: string[], options: TranslateRequest): Promise<[string[], Metadata]>;
Parameters
NameDescription
input string[]
options TranslateRequest
Returns
TypeDescription
Promise<[string[], Metadata]>

translate(input, to)

translate(input: string, to: string): Promise<[string, Metadata]>;
Parameters
NameDescription
input string
to string
Returns
TypeDescription
Promise<[string, Metadata]>

translate(input, to)

translate(input: string[], to: string): Promise<[string[], Metadata]>;
Parameters
NameDescription
input string[]
to string
Returns
TypeDescription
Promise<[string[], Metadata]>

translate(input, options, callback)

translate(input: string, options: TranslateRequest, callback: TranslateCallback<string>): void;
Parameters
NameDescription
input string
options TranslateRequest
callback TranslateCallback<string>
Returns
TypeDescription
void

translate(input, to, callback)

translate(input: string, to: string, callback: TranslateCallback<string>): void;
Parameters
NameDescription
input string
to string
callback TranslateCallback<string>
Returns
TypeDescription
void

translate(input, options, callback)

translate(input: string[], options: TranslateRequest, callback: TranslateCallback<string[]>): void;
Parameters
NameDescription
input string[]
options TranslateRequest
callback TranslateCallback<string[]>
Returns
TypeDescription
void

translate(input, to, callback)

translate(input: string[], to: string, callback: TranslateCallback<string[]>): void;
Parameters
NameDescription
input string[]
to string
callback TranslateCallback<string[]>
Returns
TypeDescription
void