Google มี ระบบแปลภาษา หรือ google translate ให้เราใช้ เท่านั้นยังไม่พอ!! พี่กู(เกิ้ล)แกยังมี goole translation/language api ให้เราเอามาเขียน
โปรแกรมกันอีกด้วย วันนี้เรามาลองเขียนโปรแกรมเพื่อแปลภาษาอังกฤษเป็นภาษาไทยกันครับ
รูปแบบ google translation/language api
URL: https://www.googleapis.com/language/translate/v2?q=go&target=th&key={YOUR_API_KEY}
– q – คำที่จะให้ google แปลง
– source – ภาษาตั้งต้น
– target – ภาษาเป้าหมายที่เราอยากให้แปลง
การทำ google translation/language api
<?php
$apiKey = '<paste your API key here>';
$text = 'Hello world!';
$url = 'https://www.googleapis.com/language/translate/v2?key=';
$url .= $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=th';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle); echo 'Source: ' . $text . '<br>';
echo 'Translation: ' . $responseDecoded['data']['translations'][0]['translatedText'];
?>