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();
CloneableInstantiable
Constants
public Google\Cloud\Translate\V2\TranslateClient::ENGLISH_LANGUAGE_CODE = 'en'
public Google\Cloud\Translate\V2\TranslateClient::FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'
public Google\Cloud\Translate\V2\TranslateClient::VERSION = '1.5.0'
Methods
public __construct(array $config = [])
 

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.

  • throws InvalidArgumentException
public detectLanguage( $string, array $options = [])
 

Detect the language of a string.

Example:

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

echo $result['languageCode'];
  • see https://cloud.google.com/translation/v2/detecting-language-with-restDetecting Langauge
  • 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.
public detectLanguageBatch(array $strings, array $options = [])
 

Detect the language of multiple strings.

Example:

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

foreach ($results as $result) {
    echo $result['languageCode'];
}
  • see https://cloud.google.com/translation/v2/detecting-language-with-restDetecting Langauge
  • 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.
public languages(array $options = [])
 

Get all supported languages.

Example:

$languages = $translate->languages();

foreach ($languages as $language) {
    echo $language;
}
  • codingStandardsIgnoreStart
  • see https://cloud.google.com/translation/v2/discovering-supported-languages-with-restDiscovering Supported Languages
  • codingStandardsIgnoreEnd
  • return array A list of supported ISO 639-1 language codes.
public localizedLanguages(array $options = [])
 

Get the supported languages for translation in the targeted language.

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'];
}
  • codingStandardsIgnoreStart
  • see https://cloud.google.com/translation/v2/discovering-supported-languages-with-restDiscovering Supported Languages
  • codingStandardsIgnoreEnd
  • 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.
public translate( $string, array $options = [])
 

Translate a string from one language to another.

Example:

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

echo $result['text'];
  • see https://cloud.google.com/translation/v2/translating-text-with-restTranslating Text
  • return 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.
public translateBatch(array $strings, array $options = [])
 

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'];
}
  • see https://cloud.google.com/translation/v2/translating-text-with-restTranslating Text
  • 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.
Properties
protected $connection
 
  • var ConnectionInterface
Methods
protected getMetaData()
 

Abstract the Metadata instantiation for unit testing

  • codeCoverageIgnore
  • return Metadata
protected isGrpcLoaded()
 

Abstract the checking of the grpc extension for unit testing.

  • codeCoverageIgnore
  • return bool
protected onGce( $httpHandler)
 

Abstract the GCECredentials call so we can mock it in the unit tests!

  • codeCoverageIgnore
  • return bool
Properties
private $key
 
  • var string
private $projectId
 
  • var string|null The project ID created in the Google Developers Console.
private $targetLanguage
 
  • var string
Methods
private configureAuthentication(array $config)
 

Fetch and validate the keyfile and set the project ID.

  • return array
private detectProjectId(array $config)
 

Detect and return a project ID.

Process:

  1. If $config['projectId'] is set, use that.
  2. If an emulator is enabled, return a dummy value.
  3. If $config['keyFile'] is set, attempt to retrieve a project ID from
    that.
  4. Check GOOGLE_CLOUD_PROJECT environment variable.
  5. Check GCLOUD_PROJECT environment variable.
  6. If code is running on compute engine, try to get the project ID from
    the metadata store.
  7. Throw exception.
  • return string
  • throws GoogleException
private getConnectionType(array $config)
 

Get either a gRPC or REST connection based on the provided config
and the system dependencies available.

  • return string
  • throws GoogleException
private getGrpcInstallationMessage()
 
  • return string
private getKeyFile(array $config = [])
 

Get a keyfile if it exists.

Process:

  1. If $config['keyFile'] is set, use that.
  2. If $config['keyFilePath'] is set, load the file and use that.
  3. If GOOGLE_APPLICATION_CREDENTIALS environment variable is set, load
    from that location and use that.
  4. If OS-specific well-known-file is set, load from that location and use
    that.
  • return array|null Key data
  • throws GoogleException
private static jsonDecode( $json, $assoc = false, $depth = 512, $options = 0)
 
  • return mixed
  • throws InvalidArgumentException
private static jsonEncode( $value, $options = 0, $depth = 512)
 
  • return string
  • throws InvalidArgumentException
private requireGrpc()
 

Throw an exception if the gRPC extension is not loaded.

  • throws GoogleException
Methods
private static jsonDecode( $json, $assoc = false, $depth = 512, $options = 0)
 
  • return mixed
  • throws InvalidArgumentException
private static jsonEncode( $value, $options = 0, $depth = 512)
 
  • return string
  • throws InvalidArgumentException
© 2020 Bruce Wells
Search Namespaces \ Classes
ConfigurationNumbers (0-9.) only