Jump to content

Recommended Posts

Link to working code? Are you serious? I just provided you with that...

 

Here you go again:

<?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('hola', 'es', 'en');
?>

 

If that doesn't work then you'll have to elaborate on how it "doesn't work".

ok what am i doing wrong here

<?
if($_POST['langpair'] == True) {   
      $textSource = $_GET['text_form'];
      $langSource = $_GET['langpair'];
           translate();
}
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('$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>Google 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> 

even this dont work

<?
if($_POST['langpair'] == True) {   
      $textSource = $_POST['text_form'];
      $langSource = $_GET['langpair'];
           translate();
}
function translate($textSource, $langSource)
{
$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) .,
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 page</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> 

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.