bulk mode
lenar opened this issue · comments
Lenar Lõhmus commented
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)
Vincent Composieux commented
@lenar Hi, thank your for your feedback, I will work on this great idea as soon as possible.
Vincent Composieux commented
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 :-)