add translation
This commit is contained in:
parent
a9484484d5
commit
bdaebf68cb
132
index.php
132
index.php
@ -419,7 +419,7 @@ if (isset($_GET['action']) || isset($_POST['action'])) {
|
||||
|
||||
if ($type === 'purchase') {
|
||||
$stmt = db()->prepare("SELECT pr.*, c.name as party_name FROM purchase_returns pr LEFT JOIN customers c ON pr.supplier_id = c.id WHERE pr.id = ?");
|
||||
$stmt->execute([$return_id]);
|
||||
$stmt->execute([return_id]);
|
||||
$return = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if ($return) {
|
||||
$stmtItems = db()->prepare("SELECT pri.*, i.name_en, i.name_ar, i.sku FROM purchase_return_items pri JOIN stock_items i ON pri.item_id = i.id WHERE pri.return_id = ?");
|
||||
@ -439,6 +439,35 @@ if (isset($_GET['action']) || isset($_POST['action'])) {
|
||||
echo json_encode($return);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($action === 'translate') {
|
||||
header('Content-Type: application/json');
|
||||
require_once __DIR__ . '/ai/LocalAIApi.php';
|
||||
$text = $_POST['text'] ?? '';
|
||||
$target = $_POST['target'] ?? 'ar';
|
||||
|
||||
if (empty($text)) {
|
||||
echo json_encode(['success' => false, 'error' => 'No text provided']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$prompt = "Translate the following product name to " . ($target === 'ar' ? 'Arabic' : 'English') . ". Return ONLY the translation, no extra text or explanations.\n\nText: " . $text;
|
||||
|
||||
$resp = LocalAIApi::createResponse([
|
||||
'input' => [
|
||||
['role' => 'system', 'content' => 'You are a translation assistant. You translate product names between English and Arabic.'],
|
||||
['role' => 'user', 'content' => $prompt],
|
||||
],
|
||||
]);
|
||||
|
||||
if (!empty($resp['success'])) {
|
||||
$translated = trim(LocalAIApi::extractText($resp));
|
||||
echo json_encode(['success' => true, 'translated' => $translated]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => $resp['error'] ?? 'Translation failed']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect to login if not authenticated
|
||||
@ -3436,11 +3465,21 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Name (EN)</label>
|
||||
<input type="text" name="name_en" class="form-control" value="<?= htmlspecialchars($item['name_en']) ?>" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_en" id="editItemNameEn<?= $item['id'] ?>" class="form-control" value="<?= htmlspecialchars($item['name_en']) ?>" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="editItemNameAr<?= $item['id'] ?>" data-target="editItemNameEn<?= $item['id'] ?>" data-to="en">
|
||||
<i class="bi bi-translate"></i> EN
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Name (AR)</label>
|
||||
<input type="text" name="name_ar" class="form-control" value="<?= htmlspecialchars($item['name_ar']) ?>" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_ar" id="editItemNameAr<?= $item['id'] ?>" class="form-control" value="<?= htmlspecialchars($item['name_ar']) ?>" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="editItemNameEn<?= $item['id'] ?>" data-target="editItemNameAr<?= $item['id'] ?>" data-to="ar">
|
||||
<i class="bi bi-translate"></i> AR
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Category</label>
|
||||
@ -8470,11 +8509,21 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Name (EN)" data-ar="الاسم (إنجليزي)">Name (EN)</label>
|
||||
<input type="text" name="name_en" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_en" id="addItemNameEn" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addItemNameAr" data-target="addItemNameEn" data-to="en">
|
||||
<i class="bi bi-translate"></i> EN
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Name (AR)" data-ar="الاسم (عربي)">Name (AR)</label>
|
||||
<input type="text" name="name_ar" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_ar" id="addItemNameAr" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addItemNameEn" data-target="addItemNameAr" data-to="ar">
|
||||
<i class="bi bi-translate"></i> AR
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label" data-en="Category" data-ar="الفئة">Category</label>
|
||||
@ -8738,11 +8787,21 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<div class="modal-body">
|
||||
<div class="mb-3">
|
||||
<label class="form-label" data-en="Name (EN)" data-ar="الاسم (إنجليزي)">Name (EN)</label>
|
||||
<input type="text" name="name_en" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_en" id="addCatNameEn" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addCatNameAr" data-target="addCatNameEn" data-to="en">
|
||||
<i class="bi bi-translate"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" data-en="Name (AR)" data-ar="الاسم (عربي)">Name (AR)</label>
|
||||
<input type="text" name="name_ar" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_ar" id="addCatNameAr" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addCatNameEn" data-target="addCatNameAr" data-to="ar">
|
||||
<i class="bi bi-translate"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
@ -8767,7 +8826,12 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Name (EN)" data-ar="الاسم (إنجليزي)">Name (EN)</label>
|
||||
<input type="text" name="name_en" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_en" id="addUnitNameEn" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addUnitNameAr" data-target="addUnitNameEn" data-to="en">
|
||||
<i class="bi bi-translate"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Short Name (EN)" data-ar="الاختصار (إنجليزي)">Short (EN)</label>
|
||||
@ -8775,7 +8839,12 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Name (AR)" data-ar="الاسم (عربي)">Name (AR)</label>
|
||||
<input type="text" name="name_ar" class="form-control" required>
|
||||
<div class="input-group">
|
||||
<input type="text" name="name_ar" id="addUnitNameAr" class="form-control" required>
|
||||
<button class="btn btn-outline-secondary btn-translate" type="button" data-source="addUnitNameEn" data-target="addUnitNameAr" data-to="ar">
|
||||
<i class="bi bi-translate"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label" data-en="Short Name (AR)" data-ar="الاختصار (عربي)">Short (AR)</label>
|
||||
@ -8838,6 +8907,51 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
});
|
||||
}
|
||||
|
||||
// Translation Logic
|
||||
document.querySelectorAll('.btn-translate').forEach(btn => {
|
||||
btn.addEventListener('click', function() {
|
||||
const sourceId = this.getAttribute('data-source');
|
||||
const targetId = this.getAttribute('data-target');
|
||||
const targetLang = this.getAttribute('data-to');
|
||||
const sourceText = document.getElementById(sourceId).value;
|
||||
|
||||
if (!sourceText) {
|
||||
alert(targetLang === 'ar' ? 'يرجى إدخال النص أولاً' : 'Please enter text first');
|
||||
return;
|
||||
}
|
||||
|
||||
const originalHtml = this.innerHTML;
|
||||
this.disabled = true;
|
||||
this.innerHTML = '<span class="spinner-border spinner-border-sm"></span>';
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('action', 'translate');
|
||||
formData.append('text', sourceText);
|
||||
formData.append('target', targetLang);
|
||||
|
||||
fetch('index.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
document.getElementById(targetId).value = data.translated;
|
||||
} else {
|
||||
alert('Translation error: ' + (data.error || 'Unknown error'));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
alert('Connection error');
|
||||
})
|
||||
.finally(() => {
|
||||
this.disabled = false;
|
||||
this.innerHTML = originalHtml;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelectorAll('.isPromotionToggleEdit').forEach(toggle => {
|
||||
toggle.addEventListener('change', function() {
|
||||
const id = this.getAttribute('data-id');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user