Budget: 500 UAH Deadline: 5 days
Your Google Translate API
$key = 'YOUR_API_KEY'
Function for translation of text
function translate($text, $source, $target) {
the global $key;
$url = 'https://translation.googleapis.com/language/translate/v2?key=' .The Key;
$data = array(
‘q’ = $text,
‘source’ = $source
‘target’ = $target
and the
$options = array(
‘HTTP’ = array(
'header' => 'Content-type: application/x-www-form-urlencoded',
‘method’ = ‘post’
'content' => http_build_query ($data)
and
and the
$context = stream_context_create ($options)
$result = file_get_contents ($url, false, $context)
if ($result === false) {
The return false.
The
$result = json_decode ($result, true)
if (!isset($result['data']['translations'][0]['translatedText'])) {
The return false.
The
return $result['data']['translations'][0]['translatedText']
The
Examples of use
$text = ‘Hello, world!‘;
$source = ‘en’;
$target = ‘uk’
$translation = translate ($text, $source, $target)
echo $translation; // will release "Hello, the world!"