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.
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 toa 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'];
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 tothe 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 tousing 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.
array|null
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'];
}
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 tothe 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 tousing 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.
array
detectLanguage
Detect the language of a string.
Example:
$result = $translate->detectLanguage('Hello world!');
echo $result['languageCode'];
string
string
The string to detect the language of.
options
array
[optional] Configuration Options.
array
detectLanguageBatch
Detect the language of multiple strings.
Example:
$results = $translate->detectLanguageBatch([
'Hello World!',
'My name is David.'
]);
foreach ($results as $result) {
echo $result['languageCode'];
}
strings
array
options
array
[optional] Configuration Options.
array
languages
Get all supported languages.
Example:
$languages = $translate->languages();
foreach ($languages as $language) {
echo $language;
}
options
array
[optional] Configuration Options.
array
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'];
}
options
array
Configuration Options.
↳ target
string
The language to discover supported languages for. Must be a valid ISO 639-1 language code. Defaults tothe value assigned to the client ( "en"
by default).
array
Constants
VERSION
Value: '1.5.0'
ENGLISH_LANGUAGE_CODE
Value: 'en'
FULL_CONTROL_SCOPE
Value: 'https://www.googleapis.com/auth/cloud-platform'