par
iron-Maniac » 01 août 2012, 11:21
Comme second exemple j'ai ceci:
<?php
Class GoogleTranslate
{
public function checkError($result)
{
$result = json_decode($result);
if (empty($result))
return array('error' => true, 'msgError' => 'Empty result');
if (!empty($result->error))
return array('error' => true, 'msgError' => $result->error->errors[0]->reason);
return array('error' => false, 'translation' => $result->data->translations[0]->translatedText);
}
public function translate($content, $from = 'fr', $to = 'en')
{
$data = array(
'key' => "xxxxxxxxxxxxxxxxxxxxxxx",
'q' => $content,
'source' => $from,
'target' => $to,
);
$urlTarget = 'https://www.googleapis.com/language/translate/v2';
$ch = curl_init($urlTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
$result = curl_exec($ch);
curl_close($ch);
return $this->checkError($result);
}
}
mysql_connect("localhost", "durex", "comcom") or die(mysql_error());
mysql_select_db("kikoolol") or die(mysql_error());
// chopper les produits
$result = mysql_query("SELECT *
FROM ps_product_lang
WHERE id_product =5
AND (
id_lang =1
OR id_lang =2
)")
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
$google = new GoogleTranslate();
$result = $google->translate($data[1][4], 'fr', 'en');
if ($result['error'])
echo $result['msgError'];
else
echo $result['translation'];
?>
ce code me renvoie ceci :
Code : Tout sélectionner
Content:
200 g
? Candle 100% natural wax and dabeille dhuile vgtale soy
comme traduction de ceci :
Code : Tout sélectionner
Contenu :
• 200 g
• ?Bougie 100 % naturelle, à base de cire d’abeille et d’huile végétale de soja
donc j'ai un problème avec les accents que je n'arrive pas résoudre, tout en sachant que urlencode et rawurlencode me donnent un résultat blanc, mais vraiment blanc.
Comme second exemple j'ai ceci:
[php]<?php
Class GoogleTranslate
{
public function checkError($result)
{
$result = json_decode($result);
if (empty($result))
return array('error' => true, 'msgError' => 'Empty result');
if (!empty($result->error))
return array('error' => true, 'msgError' => $result->error->errors[0]->reason);
return array('error' => false, 'translation' => $result->data->translations[0]->translatedText);
}
public function translate($content, $from = 'fr', $to = 'en')
{
$data = array(
'key' => "xxxxxxxxxxxxxxxxxxxxxxx",
'q' => $content,
'source' => $from,
'target' => $to,
);
$urlTarget = 'https://www.googleapis.com/language/translate/v2';
$ch = curl_init($urlTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
$result = curl_exec($ch);
curl_close($ch);
return $this->checkError($result);
}
}
mysql_connect("localhost", "durex", "comcom") or die(mysql_error());
mysql_select_db("kikoolol") or die(mysql_error());
// chopper les produits
$result = mysql_query("SELECT *
FROM ps_product_lang
WHERE id_product =5
AND (
id_lang =1
OR id_lang =2
)")
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
$google = new GoogleTranslate();
$result = $google->translate($data[1][4], 'fr', 'en');
if ($result['error'])
echo $result['msgError'];
else
echo $result['translation'];
?>[/php]
ce code me renvoie ceci :
[code]Content:
200 g
? Candle 100% natural wax and dabeille dhuile vgtale soy[/code]
comme traduction de ceci :
[code]Contenu :
• 200 g
• ?Bougie 100 % naturelle, à base de cire d’abeille et d’huile végétale de soja[/code]
donc j'ai un problème avec les accents que je n'arrive pas résoudre, tout en sachant que urlencode et rawurlencode me donnent un résultat blanc, mais vraiment blanc.