<?php function translate($sentence,$languagefrom,$languageto) { $homepage = file_get_contents('http://translate.google.com/translate_t'); if ($homepage==false) {$homepage='';} preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch); $botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]); preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput); $id=0; while (isset($botinput[0][$id])) { preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmp); $tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]); $tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]); $tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]); $len=strlen($tmp[0][0]); $len-=1; $tmp[0][0]=substr($tmp[0][0],0,$len); preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmpname); $tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]); $tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]); $tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]); $len=strlen($tmpname[0][0]); $len-=1; $tmpname[0][0]=substr($tmpname[0][0],0,$len); if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl'))) { $vars.=$tmpname[0][0]."=".$tmp[0][0].'&'; } unset($tmp); unset($tmpname); $id+=1; } $curl_handle=curl_init('http://translate.google.com/translate_t'); curl_setopt($curl_handle, CURLOPT_HEADER, true); curl_setopt($curl_handle, CURLOPT_FAILONERROR, true); curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox curl_setopt($curl_handle, CURLOPT_POST, true); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto); curl_setopt($curl_handle, CURLOPT_NOBODY, false); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); $buffer=strip_tags($buffer,'<div>'); preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match); $match[0][0]=strip_tags($match[0][0]); return $match[0][0]; } //now to use the translate function echo translate('This is the text','en','iw'); ?>
<?php function translate($textSource, $langSource, $langTarget) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); if ($ret['responseStatus'] != '200') { throw Exception('Translation failed.'); } return $ret['responseData']['translatedText']; } echo translate('this is the text', 'es', 'en'); ?>
ar = Arabic bg = Bulgarian ca = Catalan zh-CN = Chinese hr = Croatian cs = Czech da = Danish nl = Dutch en = English tl = Filipino fi = Finnish fr = French de = German el = Greek iw = Hebrew hi = Hindi id = Indonesian it = Italian ja = Japanese ko = Korean lv = Latvian lt = Lithuanian no = Norwegian pl = Polish pt = Portuguese ro = Romanian ru = Russian sr = Serbian sk = Slovak sl = Slovenian es = Spanish sv = Swedish uk = Ukrainian vi = Vietnamese
hi, cwarn23
( Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated )
Please post the form related to this??
Please post the form related to this??
<?php function translate($textSource, $langSource, $langTarget='') { $textSource=explode('<>',$textSource); if ($langTarget=='') {$langTarget='en';} $idz=0; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); $endresult.=$ret['responseData']['translatedText']; $idz+=2; } return $endresult; } echo "<form method='get' style='padding:0px; margin:0px;'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'></form>"; //now to use the translate function echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.</font>",'en',$_GET['language']); ?>
echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.</font>",'en',$_GET['language']);
<? $file=$_GET['page']; echo translate(file_get_contents($file),'en',$_GET['language']); ?>
<?php function translate($textSource, $langSource, $langTarget='') { $textSource=explode('<>',$textSource); if ($langTarget=='') {$langTarget='en';} $idz=0; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); $endresult.=$ret['responseData']['translatedText']; $idz+=2; } return $endresult; } echo "<form method='get' style='padding:0px; margin:0px;'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'></form>"; //now to use the translate function /* $url=$_SERVER['PHP_SELF']; $uu=$_SERVER['REQUEST_URI']; $urls .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);*/ echo $url = $_SERVER['REQUEST_URI'] ; $urls = preg_split('/\?/', $url); echo print_r($urls); echo $url = $urls[0]; //echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />'; $url1="C:\\xampp\htdocstranslator/xyz.php"; //echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>'; echo $p_file = dirname(__FILE__)."$url"; /* echo translate(file_get_contents($url1),'en',$_GET['language']); */ echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.\ </font>",'en',$_GET['language']); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> </head> <body> <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" > <span class="bluenormal"> Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for exploration. </span><br/><br/> </div> </body> </html>
<? set_time_limit(0); function translate($textSource, $langSource, $langTarget='') { if ($langTarget=='') {$langTarget='en';} if ($langSource!==$langTarget) { $textSource=preg_split('/(<|>)/',$textSource); $idz=0; $openbracket=1; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); if ($openbracket==1) { $bracket='<'; $openbracket=0; $endresult.=$ret['responseData']['translatedText'].$bracket; } else { $bracket='>'; $openbracket=1; $endresult.=$textSource[$idz].$bracket; } $idz+=1; } $endresult=preg_replace('/(.*)\</','$1',$endresult); return $endresult; } else { return $textSource; } } if (!isset($_GET['page'])) { $file='default.php'; } else { $file=$_GET['page']; } $data=file_get_contents('http://'.preg_replace('/(.*)\/[^\/]+/','$1/',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']).$file); echo translate($data,'en',$_GET['language']); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> </head> <body> <form method='get' style='padding:0px; margin:0px;' action='index.php'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'><input type=hidden value='<? echo basename($_SERVER['REQUEST_URI']); ?>' name='page'></form><br> <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" > <span class="bluenormal"> Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for exploration. </span><br/><br/> </div> </body> </html>
| DaniWeb Message | |
| Cancel Changes | |