the-botman Posted May 20, 2010 Share Posted May 20, 2010 hi guys... can someone tell me what i8 am doing wrong with my translate text code , it just dont translate <? if($_POST['langpair'] == en|zh-CN) { $from = en; $to = zh-CN; $textSource = $_GET['text_form']; $textSource = $textToTranslate; translate(); } function translate($textSource, $from, $to) { $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($from . '|' . $to), 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('$textSource', 'en', 'ar'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link rel="stylesheet" href="css/style.css" type="text/css" /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> </head> <body> <hr noshade size="1" style="color:#999" /> <h2>Translation API</h2> Enter the text to translate below: <form action='index.php' method=post id="text_form"> <input type=hidden name=hl value="en"> <input type=hidden name=ie value="iso-8859-1"> <?if (!empty($tranlation)) {?> <div class="highlight"> <h2>Translation</h2> <div class="translation"> <?=nl2br(utf8_decode($tranlation))?> </div> </div> <? } ?> <div class="highlight"> <textarea style="width:100%;" name=text rows="5" cols="100" wrap=PHYSICAL dir=ltr id=source><?=stripslashes($textToTranslate)?></textarea> <br/> <select name=langpair> <option value="ar|en">Arabic to English</option> <option value="zh|en">Chinese to English</option> <option value="zh-CN|zh-TW" class="line-above">Chinese (Simplified to Traditional)</option> <option value="zh-TW|zh-CN">Chinese (Traditional to Simplified) BETA</option> <option value="en|ar" class="line-above">English to Arabic</option> <option value="en|zh-CN">English to Chinese (Simplified)</option> <option value="en|zh-TW">English to Chinese (Traditional</option> <option value="en|fr" selected>English to French</option> <option value="en|de">English to German</option> <option value="en|it">English to Italian</option> <option value="en|ja">English to Japanese</option> <option value="en|ko">English to Korean</option> <option value="en|pt">English to Portuguese</option> <option value="en|ru">English to Russian</option> <option value="en|es">English to Spanish</option> <option value="fr|en" class="line-above">French to English</option> <option value="fr|de">French to German</option> <option value="de|en" class="line-above">German to English</option> <option value="de|fr">German to French</option> <option value="it|en" class="line-above">Italian to English</option> <option value="ja|en">Japanese to English BETA</option> <option value="ko|en">Korean to English BETA</option> <option value="pt|en">Portuguese to English</option> <option value="ru|en">Russian to English BETA</option> <option value="es|en">Spanish to English</option> </select> <input type=submit value="Translate" class="submit"> </form> </div> </div> </body> </html> ] Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 20, 2010 Share Posted May 20, 2010 Well, what IS it doing? Are you getting any errors? What debugging have you done to rule out some issues? I haveen't read through all of the code, but the very first line is wrong. This: if($_POST['langpair'] == en|zh-CN) { Should be: if($_POST['langpair'] == "en|zh-CN") { I also don't see where this value would be set: $textSource = $_GET['text_form']; Your form is using POST. Where are you setting a value on the query string? Also, don't use PHP short tags, it is a poor practice. Quote Link to comment Share on other sites More sharing options...
the-botman Posted May 20, 2010 Author Share Posted May 20, 2010 i changed that also and still notting, i am stuck with this code Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted May 20, 2010 Share Posted May 20, 2010 What is it you lost on here? Does the form page show up? Does it die on trying to translate? Quote Link to comment Share on other sites More sharing options...
ignace Posted May 20, 2010 Share Posted May 20, 2010 echo translate('$textSource', 'en', 'ar'); should be echo translate($textSource, 'en', 'ar'); Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted May 20, 2010 Share Posted May 20, 2010 echo translate('$textSource', 'en', 'ar'); should be echo translate($textSource, 'en', 'ar'); Oh good catch Quote Link to comment Share on other sites More sharing options...
the-botman Posted May 21, 2010 Author Share Posted May 21, 2010 thanks i changed that but it still wont give me the translation it just goes back to the edit box can someone please help me with this code its driving me mad Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted May 21, 2010 Share Posted May 21, 2010 try echoing a test string first, don't translate it. Just send the text and see if your form is working first. Then if that works which it looks like it should, go to the next step and test a string change from there... isolate where your issue exactly is first. I think it probably has something to do with the ajax translator not taking or returning a variable as expected, but comment that out and test before and after to isolate that as the problem first. Quote Link to comment Share on other sites More sharing options...
phpchamps Posted May 21, 2010 Share Posted May 21, 2010 1. How can you call translate without passing arguments??? pass arguments to translate function. 2. $textSource should be $_POST['text'] put error reporting on and see errors and pass arguments to functions and pass proper textsource and it will start working... Quote Link to comment Share on other sites More sharing options...
the-botman Posted May 21, 2010 Author Share Posted May 21, 2010 thanks got it working now i had to change the whole code around Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.