PHPackages                             darrynten/google-cloud-translate-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. darrynten/google-cloud-translate-php

ActiveLibrary[API Development](/categories/api)

darrynten/google-cloud-translate-php
====================================

PHP Google Cloud Translate Client with Extras

v1.0.0(9y ago)51.0k2[2 PRs](https://github.com/darrynten/google-cloud-translate-php/pulls)PHPPHP ^7.0

Since Mar 5Pushed 6y ago2 watchersCompare

[ Source](https://github.com/darrynten/google-cloud-translate-php)[ Packagist](https://packagist.org/packages/darrynten/google-cloud-translate-php)[ RSS](/packages/darrynten-google-cloud-translate-php/feed)WikiDiscussions dev Synced 2mo ago

READMEChangelog (1)Dependencies (4)Versions (3)Used By (0)

google-cloud-translate-php
--------------------------

[](#google-cloud-translate-php)

[![Travis Build Status](https://camo.githubusercontent.com/953a07ff4d595f2b58130829fb40cd275a5e7e39c25a9391d4f5b4e0f6afbea5/68747470733a2f2f7472617669732d63692e6f72672f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/953a07ff4d595f2b58130829fb40cd275a5e7e39c25a9391d4f5b4e0f6afbea5/68747470733a2f2f7472617669732d63692e6f72672f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e7376673f6272616e63683d6d6173746572)[![StyleCI Status](https://camo.githubusercontent.com/d1f85dbe62b87372a671b87e89dcdd603fb82ca8245cbdd97a4bd7208f740489/68747470733a2f2f7374796c6563692e696f2f7265706f732f38333933303531372f736869656c643f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/d1f85dbe62b87372a671b87e89dcdd603fb82ca8245cbdd97a4bd7208f740489/68747470733a2f2f7374796c6563692e696f2f7265706f732f38333933303531372f736869656c643f6272616e63683d6d6173746572)[![codecov](https://camo.githubusercontent.com/ef687a318973638a2464153dd29ffdb3f79f3ca2cfe8c6726dbd72465a6a9abd/68747470733a2f2f636f6465636f762e696f2f67682f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/darrynten/google-cloud-translate-php)[![Packagist Version](https://camo.githubusercontent.com/4aba4a655329450188a0578e7adb0252827591786e5e5d41f7be8ead5a82ee38/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e737667)](https://camo.githubusercontent.com/4aba4a655329450188a0578e7adb0252827591786e5e5d41f7be8ead5a82ee38/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e737667)[![MIT License](https://camo.githubusercontent.com/11c3c0109048eca8dbe9930fa80877549e8a70a46d6f3ef3bc08ede80eb16cb1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e737667)](https://camo.githubusercontent.com/11c3c0109048eca8dbe9930fa80877549e8a70a46d6f3ef3bc08ede80eb16cb1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f64617272796e74656e2f676f6f676c652d636c6f75642d7472616e736c6174652d7068702e737667)

An unofficial, fully unit tested Google Cloud Translate PHP client with extra sugar.

This is pretty much based on our [Google Natural Language](https://github.com/darrynten/google-natural-language-php)library and is quite similar.

PHP 7.0+

Why not just use the official one?
----------------------------------

[](#why-not-just-use-the-official-one)

The official one is great, and we actually use it in this package, it just doesn't quite have the sort of features we needed, so we wrapped it up with some extra bits.

What extra features?
--------------------

[](#what-extra-features)

### Cost Cutters

[](#cost-cutters)

The Google Cloud Translate API costs money. If you're doing anything with it at scale, you're going to have to keep an eye on your calls to make sure things aren't running away with you. It's not cheap.

That's why we introduced some cost cutting features.

#### Caching Requests

[](#caching-requests)

By default this package caches your requests, something you would have to usually do yourself.

It caches using a framework-agnostic approach, whereby it leverages any host frameworks caching mechanism, and falls back to a temporary cache if there is no cache available.

The supported frameworks are detailed in the [AnyCache](https://github.com/darrynten/any-cache) project.

Examples include Laravel, Symfony, Doctrine, Psr6 and more.

This feature is on by default and can easily be disabled.

#### Cheapskate Mode

[](#cheapskate-mode)

While not immediately clear, the Google Cloud Translate API charges per character. At the time of writing around $20 for 1m characters. (you pay for detection and translation at the same rates).

If you wish to first detect the language and then translate, you will pay twice for each character.

We're added cheapskate mode, and what this setting allows is for you to limit the amount of text used in language detection.

This feature is on by default and can easily be disabled. By default it limits language detection to a tweet worth of characters, and you can adjust the value too through the `cheapskateCount` property.

#### Additional Cost Saving Checks

[](#additional-cost-saving-checks)

If you submit a language that is not supported then you still get charged per character, so we ensure that we grab a copy of all supported languages and translation targets to make sure that you don't attempt to translate across unsupported languages, saving you money.

We also check to ensure you are not trying to translate the same target and source, which would also be expensive.

### Conveniences

[](#conveniences)

There are a few other conveniences like being able to set the target and source language, type, etc.

One use case would be running a single instance of text through multiple language attempts.

Usage
-----

[](#usage)

```
use DarrynTen\GoogleCloudTranslatePhp\GoogleCloudTranslate;

// Config options
$config = [
  'projectId' => 'my-awesome-project'  // At the very least
];

// Make a translator
$translate = new GoogleCloudTranslate($config);

// Get information
$translate->languages();
$translate->localizedLanguages();

// Detect languages
$translate->detectLanguage($string);
$translate->detectLanguageBatch([$strings]);

// Translate
$translate->translate($string);
$translate->translateBatch([$strings]);

// Set optional things
$language->setType('html');
$language->setModel('base');
$language->setTargetLanguage('en');
$language->setSourceLanguage('es');

// Extra features
$language->setCaching(false);
$language->setCheapskate(false);
$language->setCheapskateCount(50);

// Full config options
$config = [
  'projectId' => 'my-awesome-project',     // required
  'key' => 'api-key',                      // optional see note below
  'target' => 'en',                        // optional default is en
  'source' => 'en',                        // optional default is en
  'model' => 'base',                       // optional
  'type' => 'text',                        // optional
  'authCache' => \CacheItemPoolInterface,  // stores access tokens
  'authCacheOptions' => $array,            // cache config
  'authHttpHandler' => callable(),         // psr-7 auth handler
  'httpHandler' => callable(),             // psr-7 rest handler
  'keyFile' => $json,                      // content
  'keyFilePath' => $string,                // path
  'retries' => 3,                          // default is 3
  'scopes' => $array,                      // app scopes
  'cache' => $boolean,                     // cache
  'cheapskate' => $boolean,                // cheaper detection calls
  'cheapskateCount' => 100,                // how cheap?
];

// authCache, authCacheOptions, authHttpHandler and httpHandler are not
// currently implemented.
```

See [The Google Cloud Docs](https://googlecloudplatform.github.io/google-cloud-php/#/docs/v0.22.0/translate/translateclient)for more on these options and their usage.

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](https://cloud.google.com/translate/docs/translating-text#before-you-begin)to learn how to generate a key.

Options
-------

[](#options)

- `setType($type)` - Either `html` (default) or `text`
- `setModel($model)`
- `setSourceLanguage($language)` - Either ISO (`en`, `es`) or BCP-47 (`en-ZA`, `en-GB`).
- `setTargetLanguage($language)` - Either ISO or BCP-47.

If no language is provided then it is autodetected from the text and is returned with the response.

Missing Features
----------------

[](#missing-features)

Feel free to open a PR!

Usage of Google\\Cloud\\Storage\\StorageObject is presently not possible.

- Custom `authCache` and `authCacheOptions`
- Custom `httpHandler` and `authHttpHandler`

Roadmap
-------

[](#roadmap)

- Implement missing features

Acknowledgements
----------------

[](#acknowledgements)

- Open a PR and put yourself here :)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3361d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8458e77a1e51443980bc078f1df4954070d63d2f98b6174adfb6ce035a05e63f?d=identicon)[darrynten](/maintainers/darrynten)

---

Top Contributors

[![darrynten](https://avatars.githubusercontent.com/u/3657251?v=4)](https://github.com/darrynten "darrynten (16 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/darrynten-google-cloud-translate-php/health.svg)

```
[![Health](https://phpackages.com/badges/darrynten-google-cloud-translate-php/health.svg)](https://phpackages.com/packages/darrynten-google-cloud-translate-php)
```

###  Alternatives

[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)[hubspot/api-client

Hubspot API client

23414.2M16](/packages/hubspot-api-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
