Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP SRT Subtitle Translate with Google
<?php
/*namespace Statickidz;
*/
/**
* GoogleTranslate.class.php
*
* Class to talk with Google Translator for free.
*
* @package PHP Google Translate Free;
* @category Translation
* @author Adrián Barrio Andrés
* @author Paris N. Baltazar Salguero <sieg.sb@gmail.com>
* @copyright 2016 Adrián Barrio Andrés
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
* @version 2.0
* @link https://statickidz.com/
*/
/**
* Main class GoogleTranslate
*
* @package GoogleTranslate
*
*/
class GoogleTranslate
{
/**
* Retrieves the translation of a text
*
* @param string $source
* Original language of the text on notation xx. For example: es, en, it, fr...
* @param string $target
* Language to which you want to translate the text in format xx. For example: es, en, it, fr...
* @param string $text
* Text that you want to translate
*
* @return string a simple string with the translation of the text in the target language
*/
public static function translate($source, $target, $text)
{
// Request translation
$response = self::requestTranslation($source, $target, $text);
// Get translation text
// $response = self::getStringBetween("onmouseout=\"this.style.backgroundColor='#fff'\">", "</span></div>", strval($response));
// Clean translation
$translation = self::getSentencesFromJSON($response);
return $translation;
}
/**
* Internal function to make the request to the translator service
*
* @internal
*
* @param string $source
* Original language taken from the 'translate' function
* @param string $target
* Target language taken from the ' translate' function
* @param string $text
* Text to translate taken from the 'translate' function
*
* @return object[] The response of the translation service in JSON format
*/
protected static function requestTranslation($source, $target, $text)
{
// Google translate URL
$url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e";
$fields = array(
'sl' => urlencode($source),
'tl' => urlencode($target),
'q' => urlencode($text)
);
// URL-ify the data for the POST
$fields_string = "";
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
/**
* Dump of the JSON's response in an array
*
* @param string $json
* The JSON object returned by the request function
*
* @return string A single string with the translation
*/
protected static function getSentencesFromJSON($json)
{
$sentencesArray = json_decode($json, true);
$sentences = "";
foreach ($sentencesArray["sentences"] as $s) {
$sentences .= $s["trans"];
}
return $sentences;
}
}
<?php
set_time_limit(0);
if(!headers_sent())
header('X-Accel-Buffering: no');
$f = fopen('Goblin.E07-NEXT.srt', 'r');
ini_set('html_errors', 0);
$next_read = false;
$txt_en = array();
$txt_index = 0;
$last_line = array('x','x');
$index = 0;
while(!feof($f))
{
$line = trim(fgets($f, 1024));
if($index == 0)
{ //remove multibyte on first read
$bytes = unpack('C*', $line);
$line = chr(end($bytes));
}
if(empty($line)) continue;
if($next_read)
{
$txt_en[] = array(
'index' => $last_line[1],
'time' => $last_line[0],
'text' => $line
);
$next_read = false;
}else
{
$next_read = preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}[,0-9]*\s-->\s/', $line) > 0;
}
$last_line[1] = $last_line[0];
$last_line[0] = $line;
$index++;
}
header("Content-type: text/plain");
//$chunked = array_chunk($txt_en, 80);
//echo serialize($txt_en);
//echo json_encode($txt_en, JSON_PRETTY_PRINT);
include 'GoogleTranslate.php';
$counter = 0;
$to_translate = array();
foreach ($txt_en as $key => &$txt) {
//echo $key . ":" . $value['text'] . "\n";
$to_translate[$key] = $txt['text'];
$counter++;
if($counter>=80)
{
translate_multi_line($to_translate, $txt_en);
$counter = 0;
$to_translate = array();
}
}
if(!empty($to_translate))
translate_multi_line($to_translate, $txt_en);
function translate_multi_line(&$to_translate, &$txt_en)
{
$translated = GoogleTranslate::translate('en','id', implode("\n", $to_translate));
$translated = explode("\n", $translated);
$pos = 0;
foreach ($to_translate as $k => &$v)
{
$txt = &$txt_en[$k];
echo "{$txt['index']}\r\n{$txt['time']}\r\n";
if(!isset($translated[$pos]))
{
echo "[WARNING]: No Translated line found.";
}else
{
echo $translated[$pos];
}
$pos++;
echo "\r\n\r\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.