Cloud Translation V2 Client - Class TranslateClient (1.17.7)

Reference documentation and code samples for the Cloud Translation V2 Client class TranslateClient.

Google Cloud Translation provides the ability to dynamically translate text between thousands of language pairs and lets websites and programs integrate with translation service programmatically. Find more information at the the Google Cloud Translation docs.

The Google Cloud Translation API is available as a paid service. See the Pricing and FAQ pages for details.

Please note that while the Google Cloud Translation API supports authentication via service account and application default credentials like other Cloud Platform APIs, it also supports authentication via a public API access key. If you wish to authenticate using an API key, follow the before you begin instructions to learn how to generate a key.

Example:

use Google\Cloud\Translate\V2\TranslateClient;

$translate = new TranslateClient();

Namespace

Google \ Cloud \ Translate \ V2

Methods

__construct

Create a Translate client.

Note that when creating a TranslateClient instance, setting $config.projectId is not supported. To switch between projects, you must provide credentials with access to the project.

Parameters
Name Description
config array

Configuration Options.

↳ apiEndpoint string

A hostname with optional port to use in place of the service's default endpoint.

↳ key string

A public API access key.

↳ target string

The target language to assign to the client. Must be a valid ISO 639-1 language code. Defaults to "en" (English).

↳ authCache CacheItemPoolInterface

A cache used storing access tokens. Defaults to a simple in memory implementation.

↳ authCacheOptions array

Cache configuration options.

↳ authHttpHandler callable

A handler used to deliver Psr7 requests specifically for authentication.

↳ credentialsFetcher FetchAuthTokenInterface

A credentials fetcher instance.

↳ httpHandler callable

A handler used to deliver Psr7 requests. Only valid for requests sent over REST.

↳ keyFile array

The contents of the service account credentials .json file retrieved from the Google Developer's Console. Ex: json_decode(file_get_contents($path), true).

↳ keyFilePath string

The full path to your service account credentials .json file retrieved from the Google Developers Console.

↳ requestTimeout float

Seconds to wait before timing out the request. Defaults to 0 with REST and 60 with gRPC.

↳ retries int

Number of retries for a failed request. Defaults to 3.

↳ scopes array

Scopes to be used for the request.

↳ quotaProject string

Specifies a user project to bill for access charges associated with the request.

translate

See also:

Parameters
Name Description
string string

The string to translate.

options array

Configuration Options.

↳ source string

The source language to translate from. Must be a valid ISO 639-1 language code. If not provided the value will be automatically detected by the server.

↳ target string

The target language to translate to. Must be a valid ISO 639-1 language code. Defaults to the value assigned to the client ("en" by default).

↳ format string

Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are html or text. Defaults to "html".

↳ model string

The model to use for the translation request. May be nmt (for the NMT model) or base (for the PBMT model). Defaults to using the NMT model. If the NMT model is not supported for the requested language translation pair, the PBMT model will be defaulted instead. For a list of supported languages for the model types, please see the Language Support documentation.

Returns
Type Description
array|null A translation result including a `source` key containing the detected or provided language of the provided input, an `input` key containing the original string, and a `text` key containing the translated result.

translateBatch

See also:

Parameters
Name Description
strings array

An array of strings to translate.

options array

Configuration Options.

↳ source string

The source language to translate from. Must be a valid ISO 639-1 language code. If not provided the value will be automatically detected by the server.

↳ target string

The target language to translate to. Must be a valid ISO 639-1 language code. Defaults to the value assigned to the client ("en" by default).

↳ format string

Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are html or text. Defaults to "html".

↳ model string

The model to use for the translation request. May be nmt (for the NMT model) or base (for the PBMT model). Defaults to using the NMT model. If the NMT model is not supported for the requested language translation pair, the PBMT model will be defaulted instead. For a list of supported languages for the model types, please see the Language Support documentation.

Returns
Type Description
array A set of translation results. Each result includes a `source` key containing the detected or provided language of the provided input, an `input` key containing the original string, and a `text` key containing the translated result.

detectLanguage

Parameters
Name Description
string string

The string to detect the language of.

options array

[optional] Configuration Options.

Returns
Type Description
array A result including a `languageCode` key containing the detected ISO 639-1 language code, an `input` key containing the original string, and in most cases a `confidence` key containing a value between 0 - 1 signifying the confidence of the result.

detectLanguageBatch

Parameters
Name Description
strings array
options array

[optional] Configuration Options.

Returns
Type Description
array A set of results. Each result includes a `languageCode` key containing the detected ISO 639-1 language code, an `input` key containing the original string, and in most cases a `confidence` key containing a value between 0 - 1 signifying the confidence of the result.

languages

Parameter
Name Description
options array

[optional] Configuration Options.

Returns
Type Description
array A list of supported ISO 639-1 language codes.

localizedLanguages

Parameters
Name Description
options array

Configuration Options.

↳ target string

The language to discover supported languages for. Must be a valid ISO 639-1 language code. Defaults to the value assigned to the client ("en" by default).

Returns
Type Description
array A set of language results. Each result includes a `code` key containing the ISO 639-1 code for the supported language and a `name` key containing the name of the language written in the target language.

Constants

VERSION

Value: '1.5.0'

ENGLISH_LANGUAGE_CODE

Value: 'en'

FULL_CONTROL_SCOPE

Value: 'https://www.googleapis.com/auth/cloud-platform'