Created
May 1, 2024 15:47
-
-
Save guvenkarabulut/6554db8ea615aa404a1ec12b3fedd8c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './vendor/autoload.php'; | |
use Stichoza\GoogleTranslate\GoogleTranslate; | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$requestData = file_get_contents('php://input'); | |
$decodedData = json_decode($requestData, true); | |
if (isset($decodedData['text'])) { | |
$to_lang = $decodedData['to_lang']; | |
$translatedText = translateText($decodedData['text'], $to_lang); | |
header('Content-Type: application/json'); | |
echo json_encode(['translated_text' => $translatedText]); | |
exit(); | |
} else { | |
http_response_code(400); | |
echo json_encode(['error' => 'Missing text parameter']); | |
exit(); | |
} | |
} else { | |
http_response_code(405); | |
echo json_encode(['error' => 'Method Not Allowed']); | |
exit(); | |
} | |
function translateText($text, $to_lang) { | |
$tr = new GoogleTranslate(); | |
$tr->setSource('tr'); | |
$tr->setTarget($to_lang); | |
return $tr->translate($text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment