38682-vm/api/translate.php
2026-02-24 08:21:47 +00:00

30 lines
963 B
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../ai/LocalAIApi.php';
$data = json_decode(file_get_contents('php://input'), true);
$text = $data['text'] ?? '';
$target_lang = $data['target_lang'] ?? 'Arabic';
if (empty($text)) {
echo json_encode(['success' => false, 'error' => 'No text provided']);
exit;
}
$prompt = "Translate the following product name or description to $target_lang. Return ONLY the translated text, nothing else.\n\nText: $text";
$resp = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a helpful translation assistant.'],
['role' => 'user', 'content' => $prompt],
],
]);
if (!empty($resp['success'])) {
$translatedText = LocalAIApi::extractText($resp);
echo json_encode(['success' => true, 'translated_text' => trim($translatedText)]);
} else {
echo json_encode(['success' => false, 'error' => $resp['error'] ?? 'AI error']);
}