google-translate-php
google-translate-php copied to clipboard
Explode long texts for Google translation
Translation of rather long texts fails, I had to write smth like
function Translate($in)
{
$tr = new GoogleTranslate('en');
return $tr->translate($in);
}
$arr = explode("\n", $in);
$max = 0x400;
$in = '';
$out = '';
foreach($arr as $sz)
{
if ($in)
$in .= "\n";
$in .= $sz;
if ( strlen($in) >= $max )
{
$out .= Translate($in);
$in = '';
}
}
$out .= Translate($in);
It would be handy to have such a feature inside google-translate.
Note that I'm sure that my input contains plenty of \n
(it's SRT video subtitles); a public code must explode more accurately.
This one not working in my case.And i found a strange thing too,If i translate in google main page it showing exceeds limit but the same content is translated in the https://translate.google.co.in/ how can i over come this?
Hi i did like this and it seems fine...have any better idea please share thanks..
public function splitString($in,$tr){ $arr = explode(" ", $in); $max = 0x400; $final = ''; $out = ''; foreach($arr as $key => $word){ $temp = $final; if(strlen($temp .=" ".$word) > $max){ $out .= " ".$this->translateLocale($final,$tr); $final = ''; }else{ $final .=" ".$word; if($key == (count($arr)-1) ){ $out .= " ".$this->translateLocale($final,$tr); } } } return $out; }
This should be a simple iteration that I think shouldn't be included in the library.
Here is a better looking code if you're using PHP 8.1:
$tr = new GoogleTranslate();
$text = "this is example long text
with many new lines
lorem ipsum dolor
sit amet example
and so on";
$max = 2000; // Maximum characters per translation
// Create an array of chunked lines (until total count of chars reach $max)
$chunks = array_reduce(explode("\n", $text), function ($carry, $item) use ($max) {
$last = end($carry) ?: '';
if (strlen($last . $item) < $max && count($carry)) {
$carry[count($carry) - 1] .= "\n" . $item;
} else {
$carry[] = $item;
}
return $carry;
}, []);
// Translate every item in $chunks array.
$translated = array_map($tr->translate(...), $chunks);
Note that ...
is part of syntax, not an omission.