PHP Class Google\Cloud\Translate\TranslateClient

Please note that unlike most other Cloud Platform services Google Translate requires a public API access key and cannot currently be accessed with a service account or application default credentials. Follow the before you begin instructions to learn how to generate a key. Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder([ 'key' => 'YOUR_KEY' ]); $translate = $cloud->translate(); TranslateClient can be instantiated directly. use Google\Cloud\Translate\TranslateClient; $translate = new TranslateClient([ 'key' => 'YOUR_KEY' ]);
Inheritance: use trait Google\Cloud\ClientTrait
显示文件 Open project: GoogleCloudPlatform/gcloud-php

Protected Properties

Property Type Description
$connection Google\Cloud\Translate\Connection\ConnectionInterface

Public Methods

Method Description
__construct ( array $config = [] ) Create a Translate client.
detectLanguage ( string $string, array $options = [] ) : array Detect the language of a string.
detectLanguageBatch ( array $strings, array $options = [] ) : array Detect the language of multiple strings.
languages ( array $options = [] ) : array Get all supported languages.
localizedLanguages ( array $options = [] ) : array Get the supported languages for translation in the targeted language.
translate ( string $string, array $options = [] ) : array | null Translate a string from one language to another.
translateBatch ( array $strings, array $options = [] ) : array Translate multiple strings from one language to another.

Method Details

__construct() public method

Create a Translate client.
public __construct ( array $config = [] )
$config array [optional] { Configuration Options. @type string $key A public API access key. @type string $target The target language to assign to the client. Must be a valid ISO 639-1 language code. **Defaults to** `"en"` (English). @type int $retries Number of retries for a failed request. **Defaults to** `3`. @type string $projectId The project ID from the Google Developer's Console. @type CacheItemPoolInterface $authCache A cache used storing access tokens. **Defaults to** a simple in memory implementation. @type array $authCacheOptions Cache configuration options. @type callable $authHttpHandler A handler used to deliver Psr7 requests specifically for authentication. @type callable $httpHandler A handler used to deliver Psr7 requests. Only valid for requests sent over REST. @type string $keyFile The contents of the service account credentials .json file retrieved from the Google Developers Console. @type string $keyFilePath The full path to your service account credentials .json file retrieved from the Google Developers Console. @type int $retries Number of retries for a failed request. **Defaults to** `3`. @type array $scopes Scopes to be used for the request. }

detectLanguage() public method

Example: $result = $translate->detectLanguage('Hello world!'); echo $result['languageCode'];
See also: https://cloud.google.com/translate/v2/detecting-language-with-rest Detecting Langauge
public detectLanguage ( string $string, array $options = [] ) : array
$string string The string to detect the language of.
$options array [optional] { Configuration Options. @type string $format Indicates whether the string is either plain-text or HTML. Acceptable values are `html` or `text`. **Defaults to** `"html"`. }
return 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() public method

Example: $results = $translate->detectLanguageBatch([ 'Hello World!', 'My name is David.' ]); foreach ($results as $result) { echo $result['languageCode']; }
See also: https://cloud.google.com/translate/v2/detecting-language-with-rest Detecting Langauge
public detectLanguageBatch ( array $strings, array $options = [] ) : array
$strings array
$options array [optional] { Configuration Options. @type string $format Indicates whether the string is either plain-text or HTML. Acceptable values are `html` or `text`. **Defaults to** `"html"`. }
return 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() public method

Example: $languages = $translate->languages(); foreach ($languages as $language) { echo $language; }
See also: https://cloud.google.com/translate/v2/discovering-supported-languages-with-rest Discovering Supported Languages
public languages ( array $options = [] ) : array
$options array [optional] Configuration Options.
return array A list of supported ISO 639-1 language codes.

localizedLanguages() public method

Unlike {@see \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']; }
See also: https://cloud.google.com/translate/v2/discovering-supported-languages-with-rest Discovering Supported Languages
public localizedLanguages ( array $options = [] ) : array
$options array [optional] { Configuration Options. @type string $target 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). }
return 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.

translate() public method

Example: $translation = $translate->translate('Hello world!'); echo $translation['text'];
See also: https://cloud.google.com/translate/v2/translating-text-with-rest Translating Text
public translate ( string $string, array $options = [] ) : array | null
$string string The string to translate.
$options array [optional] { Configuration Options. @type string $source 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. @type string $target 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). @type string $format Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are `html` or `text`. **Defaults to** `"html"`. @type string $model The model to use for the translation request. May be `nmt` or `base`. **Defaults to** an empty string. }
return array | null A translation result including a `source` key containing the detected or provided langauge of the provided input, an `input` key containing the original string, and a `text` key containing the translated result.

translateBatch() public method

Example: $translations = $translate->translateBatch([ 'Hello world!', 'My name is David.' ]); foreach ($translations as $translation) { echo $translation['text']; }
See also: https://cloud.google.com/translate/v2/translating-text-with-rest Translating Text
public translateBatch ( array $strings, array $options = [] ) : array
$strings array An array of strings to translate.
$options array [optional] { Configuration Options. @type string $source 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. @type string $target 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). @type string $format Indicates whether the string to be translated is either plain-text or HTML. Acceptable values are `html` or `text`. **Defaults to** `"html"`. @type string $model The model to use for the translation request. May be `nmt` or `base`. **Defaults to** an empty string. }
return 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.

Property Details

$connection protected_oe property

protected ConnectionInterface,Google\Cloud\Translate\Connection $connection
return Google\Cloud\Translate\Connection\ConnectionInterface