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 | |
---|---|
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 |
↳ 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: |
↳ 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 |
↳ retries |
int
Number of retries for a failed request. Defaults to |
↳ 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 | |
---|---|
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 ( |
↳ format |
string
Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are |
↳ model |
string
The model to use for the translation request. May be |
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
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 | |
---|---|
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 ( |
↳ format |
string
Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are |
↳ model |
string
The model to use for the translation request. May be |
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
Detect the language of a string.
Example:
$result = $translate->detectLanguage('Hello world!');
echo $result['languageCode'];
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
Detect the language of multiple strings.
Example:
$results = $translate->detectLanguageBatch([
'Hello World!',
'My name is David.'
]);
foreach ($results as $result) {
echo $result['languageCode'];
}
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
Get all supported languages.
Example:
$languages = $translate->languages();
foreach ($languages as $language) {
echo $language;
}
Parameter | |
---|---|
Name | Description |
options |
array
[optional] Configuration Options. |
Returns | |
---|---|
Type | Description |
array | A 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 | |
---|---|
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 ( |
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'