Git Product home page Git Product logo

google-translate-php's Introduction

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.0"
}

Usage

Basic Usage

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');

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

Warning! This feature is experimental and works only for object calls (non-static).

$tr = new TranslateClient(null, 'fr');
$text = $tr->translate('Hello World!');
echo $tr->getLastDetectedSource(); // Output: en

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 "CURLing" the Google Translate website. Consider buying Official Google Translate API for other types of usage.

google-translate-php's People

Contributors

stichoza avatar gogromat avatar scrutinizer-auto-fixer avatar timcotten avatar subsan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.