Cloud Translation V2 Client - Class TranslateClient (1.13.3)

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

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

Translate a string from one language to another.

Example:

$result = $translate->translate('Hello world!');

echo $result['text'];
Parameters
NameDescription
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
TypeDescription
array|nullA 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

Translate multiple strings from one language to another.

Example:

$results = $translate->translateBatch([
    'Hello world!',
    'My name is David.'
]);

foreach ($results as $result) {
    echo $result['text'];
}
Parameters
NameDescription
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
TypeDescription
arrayA 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

Detect the language of a string.

Example:

$result = $translate->detectLanguage('Hello world!');

echo $result['languageCode'];
Parameters
NameDescription
string string

The string to detect the language of.

options array

[optional] Configuration Options.

Returns
TypeDescription
arrayA 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

Detect the language of multiple strings.

Example:

$results = $translate->detectLanguageBatch([
    'Hello World!',
    'My name is David.'
]);

foreach ($results as $result) {
    echo $result['languageCode'];
}
Parameters
NameDescription
strings array
options array

[optional] Configuration Options.

Returns
TypeDescription
arrayA 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

Get all supported languages.

Example:

$languages = $translate->languages();

foreach ($languages as $language) {
    echo $language;
}
Parameter
NameDescription
options array

[optional] Configuration Options.

Returns
TypeDescription
arrayA list of supported ISO 639-1 language codes.

localizedLanguages

Get the supported languages for translation in the targeted language.

Unlike Google\Cloud\Translate\V2\Google\Cloud\Translate\TranslateClient::languages() this will return the localized language names in addition to the ISO 639-1 language codes.

Example:

$languages = $translate->localizedLanguages();

foreach ($languages as $language) {
    echo $language['code'];
}
Parameters
NameDescription
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
TypeDescription
arrayA 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'