Бюджет: 500 UAH Термін: 5 днів
// Ваш ключ API Google Translate
$key = 'YOUR_API_KEY';
// Функція для перекладу тексту
function translate($text, $source, $target) {
global $key;
$url = 'https://translation.googleapis.com/language/translate/v2?key=' . $key;
$data = array(
'q' => $text,
'source' => $source,
'target' => $target,
);
$options = array(
'http' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === false) {
return false;
}
$result = json_decode($result, true);
if (!isset($result['data']['translations'][0]['translatedText'])) {
return false;
}
return $result['data']['translations'][0]['translatedText'];
}
// Приклад використання
$text = 'Hello, world!';
$source = 'en';
$target = 'uk';
$translation = translate($text, $source, $target);
echo $translation; // виведе "Привіт, світ!"