Home google translate function not working when using on ajax load page, jquery, php
Reply: 0

google translate function not working when using on ajax load page, jquery, php

user2351
1#
user2351 Published in April 22, 2018, 10:06 pm

This works on a normal page but when I have it on a ajax loaded page it does not work. Anyone know why that is? The function isn't important but for context it is below.

This is the function below

 class gtranslate {

function __construct() {
    $this->translate_url = 'https://translate.google.com/m?ie=UTF-8&prev=_m&hl=en&';
    $this->urlReferer = 'https://translate.google.com/m';
    $this->userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
}

public function translate($text, $to, $from = '',$cache=false) {
    $url = $this->translate_url . 'sl=' . $from . '&tl=' . $to . '&q=' . urlencode(@$text);

    if(file_exists('cache/'.$to.'/'.md5($url).'.cache') && $cache){
        return file_get_contents('cache/'.$to.'/'.md5($url).'.cache');
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
    curl_setopt($ch, CURLOPT_REFERER, $this->urlReferer);
    curl_setopt($ch, CURLOPT_URL, $url);
    $resp = $this->parseResponse(curl_exec($ch));
    if(!file_exists('cache')){
        @mkdir('cache');
    }
    if(!file_exists('cache/'.$to)){
        @mkdir('cache/'.$to);
    }
    @file_put_contents('cache/'.$to.'/'.md5($url).'.cache', $resp);
    return $resp;
}

private function parseResponse($str = '') {
    $result = strip_tags($str, '<div>');
    $result = explode('<', substr($result, strpos($result, 'class="t0"') + 11, strpos($result, 'class="t0"')));
    $result = $result[0];
    return $result;
}

  }

And this is how I call the function

 $gt=new gtranslate();

$translatedword = $gt->translate($theword, 'en','es',true);


echo $translatedword;

I have all of this on a page I load with the following jquery

<script>
$(document).ready(function(){
 $("span.readword").click(function(){
   // alert(event.target.textContent);
    var thecontent = event.target.textContent;
    //alert(thecontent);

   $("#wordbox").load("include/readbox.php", {theword: thecontent});

   });
});
  </script>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32027 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO