Files
citrus/test_google_translate.php
T

33 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
function translateText($text, $from = 'tr', $to = 'en') {
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" . $from . "&tl=" . $to . "&dt=t&q=" . urlencode($text);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
curl_close($ch);
if (!$response) {
return false;
}
$data = json_decode($response, true);
if (!isset($data[0])) {
return false;
}
$translated = "";
foreach ($data[0] as $sentence) {
$translated .= $sentence[0];
}
return $translated;
}
$test = translateText("Merhaba Dünya! Web Uygulamaları ve Nesnelerin İnterneti geliştirmekteyiz.");
echo "Original: Merhaba Dünya! Web Uygulamaları ve Nesnelerin İnterneti geliştirmekteyiz.\n";
echo "Translated: " . $test . "\n";