GithubHelp home page GithubHelp logo

eko / googletranslatebundle Goto Github PK

View Code? Open in Web Editor NEW
44.0 5.0 22.0 73 KB

A Symfony bundle to deals with Google Translate API

License: MIT License

PHP 78.01% HTML 21.99%
symfony symfony-bundle language translation-service detector detect-language

googletranslatebundle's Introduction

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.

googletranslatebundle's People

Contributors

2no avatar albertomr86 avatar eko avatar jeffclemens avatar liviucmg avatar mho79 avatar mickaelandrieu avatar tobias-nitsche avatar whyte624 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

googletranslatebundle's Issues

API Key in config.yml

Hi,

I am trying to make a request but I always get the error

No method can handle the key config key

Any idea why this happens? I have my key configured in the config.yml file. I am guessing this is the key from my Google Developer Console. SHould I use the server or the browser API Key? Let me know.

not install, not compatible guzzle

composer require eko/googletranslatebundle
Using version ^1.1 for eko/googletranslatebundle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- eko/googletranslatebundle 1.1.2 requires guzzlehttp/guzzle 5.0.@dev -> satisfiable by guzzlehttp/guzzle[5.0.0, 5.0.1, 5.0.2, 5.0.3] but these conflict with your requirements or minimum-stability.
- eko/googletranslatebundle 1.1.1 requires guzzlehttp/guzzle 5.0.
@dev -> satisfiable by guzzlehttp/guzzle[5.0.0, 5.0.1, 5.0.2, 5.0.3] but these conflict with your requirements or minimum-stability.
- eko/googletranslatebundle 1.1.0 requires guzzlehttp/guzzle 5.0.*@dev -> satisfiable by guzzlehttp/guzzle[5.0.0, 5.0.1, 5.0.2, 5.0.3] but these conflict with your requirements or minimum-stability.
- Installation request for eko/googletranslatebundle ^1.1 -> satisfiable by eko/googletranslatebundle[1.1.0, 1.1.1, 1.1.2].

Installation failed, reverting ./composer.json to its original content.

bulk mode

Support for translating multiple strings at once (with one request having multiple q= parameters to google) would be nice.

    /**
     * Translates given string in given target language from a source language via the Google Translate API.
     * If source language is not defined, it use detector method to detect string language
     *
     * @param string|array $query  A query string to translate
     * @param string $target A target language
     * @param string $source A source language
     *
     * @return string|array
     */
    public function translate(/* string | array */ $query, $target, $source = null)

New tag

Hi, could you tag the current version (including #10) ? Thank you.

Issues Call Google Api Key

Hi all,
I'm trying to use this service on my application but, i have this problem :

Client error response [url] https://www.googleapis.com/language/translate/v2/detect?key=[key]&q=Hello [status code] 400 [reason phrase] Bad Request

This is my controller :
$translator = $this->get('eko.google_translate.detector');
$translatedString = $translator->detect('Hello');
print_r($translatedString);
die();

And this is my config :
eko_google_translate:
api_key: my_api_key

So i don't know where is the problem exactly, but i thing that it's with my Google Translator Api Key.
Some helps please ?

API Key in config.yml

So I should just do a composer update right? The error still occurs.

My config.yml entry is like this:
eko_google_translate:
api_key: server_public_api_key_generated_in_google_developers_console

My controller code is:

$translator = $this->get('eko.google_translate.translator');
$value = $translator->translate($entity->getContent(), $language->getIsoCode(), 'en');

Translate texts longer than 2000 bytes

Hi there,

The Google API allows for a maximum of 2K characters per query, if you're using GET. See here:
https://cloud.google.com/translate/v2/using_rest#detect-WorkingResults

If you want to translate longer texts, one solution is to split it up into multiple queries. The split should be done at key characters, such as newlines (end of paragraphs), dots (end of sentences) or at worst at spaces (end of a word). I am going to submit a pull request for an implementation soon.

Another solution would be to use POST instead of GET, which apparently raises this limit to 5K characters.

Cheers!

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.