How to translate a text one langugae to other langugae in php
In php if you want to translate a text from one language to other language then you can use below code. you have to create a google api key for translate word.In this we should pass a source language code or target language code or a text string.
Use Below Code :
function translateText($source, $target, $text) { $apiKey = 'xxxxxyCdGtTIxxxxxxfanlujqxxxxx'; $url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source='.$source.'&target='.$target.''; $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($handle); $responseDecoded = json_decode($response, true); curl_close($handle); $response = (isset($responseDecoded['data']) && !empty($responseDecoded['data']))?$responseDecoded['data']['translations'][0]['translatedText']:''; return $response; } $data = translateText("en","it","Hello World"); Output : Ciao mondo
Share