<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Google Translator Service</title> </head> <body> <?php require ("google.translator.class.php"); function bool_to_str($val){ if ($val) return "yes"; else return "no"; } //The key, I never chek this if this api need any key at all, leave it be. $translator=new GTranslator(); if (isset($_POST['translateme'])){ echo "<h1>detect language result</h1>"; //You can check any thing here, for me, in this example its enaugh to chek the results... $detect= $translator->detect_language($_POST['translateme']); $responseData=$detect['responseData']; if (is_array($responseData)){ echo "Detected language is : " . $responseData['language'] . ' - ' . $translator->get_name_of($responseData['language']) .'<br />'; echo "Is reliable : " . bool_to_str( $responseData['isReliable']). "<br />"; echo "confidence : " . $responseData['confidence']. "<br />"; }else { echo "Error : ". $responseData['responseDetails'] . "<br />" ; } echo "<h1>Translate result</h1>"; echo "<em>From this version, this function generate a USER_NOTICE, use translate2 function! </em><br />"; $trans=@$translator->translate($_POST['translateme'],$_POST['from_lang'],$_POST['to_lang']); $responseData=$trans['responseData']; if (is_array($responseData)){ if (isset($responseData['detectedSourceLanguage'])) echo "Detected language is : " . $responseData['detectedSourceLanguage'] . ' - ' . $translator->get_name_of($responseData['detectedSourceLanguage']) .'<br />'; echo "Translated text :<br />"; echo $responseData['translatedText']; }else { echo "Error : ". $responseData['responseDetails'] . "<br />" ; } echo "<h1>Translate2 result</h1>"; $trans=$translator->translate2($_POST['translateme'],$_POST['from_lang'],$_POST['to_lang']); //This is not so much tested, Do your own test on this array! $translation=$trans[0]; $suggestion=$trans[1]; $sourceLanguage=$trans[2]; echo "Source text : ",$translation[0][1],"<br />"; echo "Translated text : ",$translation[0][0],"<br />"; if (is_array($suggestion)){ echo "<pre>"; foreach ($suggestion as $suggest){ echo "<strong>{$suggest[0]}:\n</strong>"; $j=0; foreach ($suggest[1] as $s) echo ++$j,":",$s,"\n"; } echo "</pre>"; } echo "Source language : ",$sourceLanguage; } $valids=$translator->get_valid_language(); $select=""; foreach ($valids as $lang_id=>$lang_name){ if ($lang_id!="auto") $select.="<option value='$lang_id'>$lang_name</option>\n"; } ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" > <label for="translateme" style="display:block">Translate this : </label><textarea style="width:300px;height:100px" name="translateme"></textarea> <label for="from_lang" style="display:block">From:<label><select name="from_lang"><option value="auto">Detect language</option><?=$select?></select> <label for="to_lang" style="display:block">To:<label><select name="to_lang"><?=$select?></select> <br /><input type="submit" value="Translate" name="translate" /> </form> </body> </html>
|