ccc

Google Translate con PHP

GoogleTranslate.php:
class GoogleTranslate {
    public static function translate($source, $target, $text) {
        $response = self::requestTranslation($source, $target, $text);
        $translation = self::getSentencesFromJSON($response);
        return $translation;
    }
 
    protected static function requestTranslation($source, $target, $text) {
        $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)
        );

        if(strlen($fields['q'])>=5000)
            throw new \Exception("Maximum number of characters exceeded: 5000");
     
        $fields_string = "";
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }

        rtrim($fields_string, '&');

        $ch = curl_init();

        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');

        $result = curl_exec($ch);

        curl_close($ch);

        return $result;
    }

    protected static function getSentencesFromJSON($json)  {
        $sentencesArray = json_decode($json, true);
        $sentences = "";

        foreach ($sentencesArray["sentences"] as $s) {
            $sentences .= isset($s["trans"]) ? $s["trans"] : '';
        }

        return $sentences;
    }
}

prueba.php:
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="iso-8859-1">
</head>
<body>

<script>
var idLangActivo = "es";
</script>
<?php
require("GoogleTranslate.php");
$text = 'Don Quijote de la Mancha es una novela escrita por el español Miguel de Cervantes Saavedra. Publicada su primera parte con el título de El ingenioso hidalgo don Quijote de la Mancha a comienzos de 1605, es la obra más destacada de la literatura española y una de las principales de la literatura universal. En 1615 apareció su continuación con el título de';
 ?>

 <div id="zonaCarga"><?=$text;?></div>

 <div>
<select id="comboLang">
<option></option>
<option value="es">Español</option>
<option value="en">Inglés</option>
<option value="fr">Francés</option>
<option value="pt">Portugués</option>
</select>
 </div>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<script>
$(function() {
$("#comboLang").change(function(){
idLang = $("#comboLang option:selected").val();
$.ajax({
async: false,
type: "POST",
url: "getTextoLang.php",
data: {
idLang: idLang,
idLangActivo: idLangActivo,
texto: $("#zonaCarga").html()
},
success: function(data) {
idLangActivo = idLang
$("#zonaCarga").html(data)
  },
error: function(data){
}
});
});
});
</script>
</body>
</html>

getTextoLang.php:
require("GoogleTranslate.php");
$trans = new GoogleTranslate();
$result = $trans->translate($_POST["idLangActivo"], $_POST["idLang"], $_POST["texto"]);
echo utf8_decode($result);

No hay comentarios:

Publicar un comentario