Developed with love by KnpLabs Hire us for your project!
30

GoogleTranslateBundle

by eko

A Symfony bundle to deals with Google Translate API

GoogleTranslateBundle

SensioLabsInsight

Build Status
Latest Stable Version
Total Downloads

Features

  • Detect language used for a string
  • Translate a string from a source language to a target one
  • Translate a string into a target language by using language auto-detection (consume 1 more API call)
  • Retrieve all languages available on API and obtain language names in a given language
  • Profile detector / translate / languages list API calls in the Symfony profiler!

Installation

Add the bundle to your composer.json file:

{
    "require" :  {
        "eko/googletranslatebundle": "dev-master"
    }
}

Add this to app/AppKernel.php

<?php
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Eko\GoogleTranslateBundle\EkoGoogleTranslateBundle(),
        );

        ...

        return $bundles;
    }

Configuration

Edit app/config.yml

The following configuration lines are required:

eko_google_translate:
    api_key: <your key api string>

Usages

Detect a string language

Retrieve the detector service and call the detect() method:

$detector = $this->get('eko.google_translate.detector');
$value = $detector->detect('Hi, this is my string to detect!');
// This will return 'en'

Translate a string

Retrieve the translator service and call the translate() method:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate('Hi, this is my text to detect!', 'fr', 'en');
// This will return 'Salut, ceci est mon texte à détecter!'

Translate a string from unknown language (use detector)

Retrieve the translator service and call the translate() method without the source (third) parameter:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate('Hi, this is my text to detect!', 'fr');
// This will return 'Salut, ceci est mon texte à détecter!'

Translate multiple strings

Retrieve the translator service and call the translate() method with an array of your strings:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate(array('Hi', 'This is my second text to detect!'), 'fr', 'en');
// This will return the following array:
// array(
//     0 => 'Salut',
//     1 => 'Ceci est mon second texte à détecter !',
// )

Note that you can also use an "economic mode" to translate multiple strings in a single request which is better for your application performances.

Your translations will be concatenated in one single Google request. To use it, simply add true to the last argument:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate(array('Hi', 'This is my second text to detect!'), 'fr', 'en', true);
// This will return the following array:
// array(
//     0 => 'Salut',
//     1 => 'Ceci est mon second texte à détecter !',
// )

Obtain all languages codes available

Retrieve the languages service and call the get() method without any argument:

$languages = $this->get('eko.google_translate.languages')->get();
// This will return:
// array(
//     array('language' => 'en'),
//     array('language' => 'fr'),
//     ...
// )

Obtain all languages codes available with their names translated

Retrieve the languages service and call the get() method with a target language argument:

$languages = $this->get('eko.google_translate.languages')->get('fr');
// This will return:
// array(
//     array('language' => 'en', 'name' => 'Anglais'),
//     array('language' => 'fr', 'name' => 'Français'),
//     ...
// )

Notice: this will consume a detector API call.

The MIT License (MIT)

Copyright (c) 2013-2014 Vincent Composieux <vincent.composieux@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
eko_google_translate:
api_key: ~ # Required
  • Fix .travis.yml file to do not test on Symfony 3
    By eko, 2 years ago
  • Add new line at end of file (SensioLabs Insight fix)
    By eko, 2 years ago
  • Fix #6 by fixing the Languages API method query call
    By eko, 2 years ago
  • Add Symfony 2.6 to travis builds
    By eko, 2 years ago
  • Update Symfony composer dependency to allow Symfony 3
    By eko, 2 years ago
  • Merge pull request #5 from whyte624/master
    By eko, 2 years ago
  • updates for new guzzlehttp dependency
    By , 2 years ago
  • Update travis-ci configuration file by removing php 5.3, adding 5.6 and hhvm-nightly
    By eko, 2 years ago
  • Move from old guzze/guzzle to new guzzlehttp/guzzle dependency
    By eko, 2 years ago
  • Add new line at end of files
    By eko, 2 years ago
  • Move to PSR-4 autoloading
    By eko, 3 years ago
  • Update composer.json symfony requirements
    By eko, 3 years ago
  • Update Travis CI testing php/symfony versions and environments
    By eko, 3 years ago
  • Fix non-usued variable
    By eko, 3 years ago
  • Add multiple translations (fix issue #3)
    By eko, 3 years ago
  • Merge pull request #2 from wakuworks/master
    By eko, 4 years ago
  • Remove unnecessary url encoded.
    By wakuworks, 4 years ago
  • Fix kernel.debug parameter that always exists
    By eko, 4 years ago
  • Fix some little errors
    By eko, 4 years ago
  • Add symfony profiler on API methods
    By eko, 4 years ago
  • Typo fix
    By eko, 4 years ago
  • Add insight medal in README file
    By eko, 4 years ago
  • Update composer.json Guzzle version
    By eko, 4 years ago
  • Typo fix
    By eko, 4 years ago
  • Add languages list API call
    By eko, 4 years ago
  • Update filemodes to 655
    By eko, 4 years ago
  • Update units tests to mock Google Translate API response
    By eko, 4 years ago
  • Update documentation to add composer install
    By eko, 4 years ago
  • Fix email address
    By eko, 4 years ago
  • Merge pull request #1 from mickaelandrieu/patch-1
    By eko, 4 years ago