eko / GoogleTranslateBundle

A Symfony bundle to deals with Google Translate API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bulk mode

lenar opened this issue · comments

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)

@lenar Hi, thank your for your feedback, I will work on this great idea as soon as possible.

Hi,

This is now done. You can update code and send an array to the translate method, like that:

$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 I've also introduced an "economic mode" to translate multiple strings in a single request.

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

$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 !',
// )

Thank you for your feedback and have a great time using this bundle :-)