Jump to content

Recommended Posts

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> 
]

 

 

Link to comment
https://forums.phpfreaks.com/topic/202386-what-am-i-doing-wrong/
Share on other sites

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.

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.

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.