Skip to content

Latest commit

History

History
137 lines (95 loc) 路 5.35 KB

README.md

File metadata and controls

137 lines (95 loc) 路 5.35 KB

Google Translate PHP SensioLabsInsight

Build Status Latest Stable Version Total Downloads Downloads Month Code Climate Scrutinizer Code Quality

Free Google Translate API PHP Package. Translates totally free of charge.

Installation

Install this package via Composer.

composer require stichoza/google-translate-php

Or edit your project's composer.json to require stichoza/google-translate-php and then run composer update.

"require": {
    "stichoza/google-translate-php": "~3.2"
}

Usage

Basic Usage

Note: You should have composer's autoloader included require 'vendor/autoload.php' (that's obvious.)

Instantiate TranslateClient object

use Stichoza\GoogleTranslate\TranslateClient;

$tr = new TranslateClient('en', 'ka');

Or set/change languages later

$tr = new TranslateClient(); // Default is from 'auto' to 'en'
$tr->setSource('en'); // Translate from English
$tr->setTarget('ka'); // Translate to Georgian

Translate sentences

echo $tr->translate('Hello World!');

Also, you can also use method chaining

echo $tr->setSource('en')->setTarget('ka')->translate('Goodbye');

Or call a static method

echo TranslateClient::translate('en', 'ka', 'Hello again');

As of v3.2 multiple sentence/array translation is available.

echo $tr->translate(['I can dance', 'I like trains', 'Double rainbow']);

Advanced Configuration

This package uses Guzzle for HTTP requests. You can pass an associative array of guzzle client configuration options as a third parameter to TranslateClient constructor.

You can configure proxy, user-agent, default headers, connection timeout and so on using this options.

$tr = new TranslateClient(null, 'en', [
    'defaults' => [
        'timeout' => 10,
        'proxy' => [
            'http'  => 'tcp://localhost:8125',
            'https' => 'tcp://localhost:9124'
        ],
        'headers' => [
            'User-Agent' => 'Foo/5.0 Lorem Ipsum Browser'
        ]
    ]
]);

For more information, see Creating a Client section in Guzzle docs (6.x version).

Language Detection

To detect language automatically, just set the source language to null

$tr = new TranslateClient(null, 'es'); // Detect language and translate to Spanish
$tr->setSource(null); // Another way

Get Detected Language

You can also use getLastDetectedSource() method both statically and non-statically to get detected language.

$tr = new TranslateClient(null, 'fr');

$text = $tr->translate('Hello World!');

echo $tr->getLastDetectedSource();             // Output: en
echo TranslateClient::getLastDetectedSource(); // Output: en

Note: Value of last detected source is same for both static and non-static method calls.

Return value may be boolean FALSE if there is no detected language.

Available languages

Supported languages are listed in Google API docs.

Errors and Exception Handling

Both static and non-static translate() methods will throw following Exceptions:

  • InvalidArgumentException If parameters are passed incorrectly.
  • ErrorException If the HTTP request fails for some reason.
  • UnexpectedValueException If data received from Google cannot be decoded.
  • BadMethodCallException If you call something wrong. Call translate(), not Ghost Busters

In addition translate() method will return boolean FALSE if there is no translation available.

Older versions

You can still view usage docs for older (~2.0) versions here.

Disclaimer

This package is developed for educational purposes only. Do not depend on this package as it may break anytime as it is based on crawling the Google Translate website. Consider buying Official Google Translate API for other types of usage.

Also, Google might ban your server IP or requre to solve CAPTCHA if you send unusual traffic (large amount of data/requests).