amine_dubs commited on
Commit
4a21477
·
1 Parent(s): 52c54ab
Files changed (1) hide show
  1. static/script.js +85 -61
static/script.js CHANGED
@@ -340,18 +340,19 @@ window.onload = function() {
340
  docTranslationForm.addEventListener('submit', function(e) {
341
  e.preventDefault();
342
 
343
- const fileInput = document.getElementById('doc-input');
344
- if (!fileInput.files || fileInput.files.length === 0) {
345
  showError('Please select a document to translate.');
346
  return;
347
  }
348
 
349
- const file = fileInput.files[0];
350
- const sourceLang = sourceLangDoc.value;
351
- const targetLang = targetLangDoc.value;
352
 
353
  translateDocument(file, sourceLang, targetLang);
354
  });
 
 
355
  }
356
 
357
  // File drag and drop
@@ -572,42 +573,78 @@ window.onload = function() {
572
 
573
  // Function to download translated document
574
  function downloadTranslatedDocument(content, fileName, fileType) {
575
- console.log('Downloading translated document:', fileName, fileType);
576
- // Determine the file extension
577
- let extension = '';
578
- if (fileName.toLowerCase().endsWith('.pdf')) {
579
- extension = '.pdf';
580
- } else if (fileName.toLowerCase().endsWith('.docx')) {
581
- extension = '.docx';
582
- } else if (fileName.toLowerCase().endsWith('.txt')) {
583
- extension = '.txt';
584
- } else {
585
- extension = '.txt'; // Default to txt
586
- }
587
 
588
- // Create file name for translated document
589
  const baseName = fileName.substring(0, fileName.lastIndexOf('.'));
590
  const translatedFileName = `${baseName}_translated${extension}`;
591
 
592
- // Show notification that download is starting
593
- showNotification('Preparing document for download...');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
 
595
- // For text files, we can download directly from the browser
596
  if (extension === '.txt') {
597
- const blob = new Blob([content], { type: 'text/plain' });
 
598
  const url = URL.createObjectURL(blob);
599
-
600
- const a = document.createElement('a');
601
- a.href = url;
602
- a.download = translatedFileName;
603
- document.body.appendChild(a);
604
- a.click();
605
- document.body.removeChild(a);
606
- URL.revokeObjectURL(url);
607
-
608
- showNotification('Document downloaded successfully!');
609
  } else {
610
- // For PDF and DOCX files, we need the server to create them
611
  fetch('/download/translated-document', {
612
  method: 'POST',
613
  headers: {
@@ -616,48 +653,35 @@ window.onload = function() {
616
  body: JSON.stringify({
617
  content: content,
618
  filename: translatedFileName,
619
- original_type: fileType || 'text/plain'
620
  }),
621
  })
622
  .then(response => {
623
  if (!response.ok) {
624
- throw new Error(`Server error: ${response.status} ${response.statusText}`);
625
  }
626
  return response.blob();
627
  })
628
  .then(blob => {
629
- // Create appropriate MIME type based on extension
630
- let mimeType;
631
- if (extension === '.pdf') {
632
- mimeType = 'application/pdf';
633
- } else if (extension === '.docx') {
634
- mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
635
- } else {
636
- mimeType = 'text/plain';
637
- }
638
-
639
- // Create a blob with the correct MIME type
640
- const fileBlob = new Blob([blob], { type: mimeType });
641
- const url = URL.createObjectURL(fileBlob);
642
-
643
- // Create and trigger download link
644
- const a = document.createElement('a');
645
- a.href = url;
646
- a.download = translatedFileName;
647
- document.body.appendChild(a);
648
- a.click();
649
- document.body.removeChild(a);
650
- URL.revokeObjectURL(url);
651
-
652
- showNotification('Document downloaded successfully!');
653
  })
654
  .catch(error => {
655
- console.error('Error downloading document:', error);
656
- showError(`Download error: ${error.message}`);
657
  });
658
  }
659
  }
660
 
 
 
 
 
 
 
 
 
 
 
661
  // Helper function to get language name from code
662
  function getLanguageName(code) {
663
  // Hard-coded mapping for common languages
 
340
  docTranslationForm.addEventListener('submit', function(e) {
341
  e.preventDefault();
342
 
343
+ const file = document.getElementById('doc-input').files[0];
344
+ if (!file) {
345
  showError('Please select a document to translate.');
346
  return;
347
  }
348
 
349
+ const sourceLang = document.getElementById('doc-source-lang').value;
350
+ const targetLang = document.getElementById('doc-target-lang').value;
 
351
 
352
  translateDocument(file, sourceLang, targetLang);
353
  });
354
+ } else {
355
+ console.error('Document translation form not found.');
356
  }
357
 
358
  // File drag and drop
 
573
 
574
  // Function to download translated document
575
  function downloadTranslatedDocument(content, fileName, fileType) {
576
+ // Determine file extension
577
+ let extension = fileName.endsWith('.pdf') ? '.pdf' :
578
+ fileName.endsWith('.docx') ? '.docx' : '.txt';
 
 
 
 
 
 
 
 
 
579
 
580
+ // Create translated filename
581
  const baseName = fileName.substring(0, fileName.lastIndexOf('.'));
582
  const translatedFileName = `${baseName}_translated${extension}`;
583
 
584
+ // For PDF files, try the browser's native PDF generation when it contains Arabic
585
+ if (extension === '.pdf' && /[\u0600-\u06FF]/.test(content)) {
586
+ console.log('Using browser PDF generation for Arabic content');
587
+
588
+ // Create HTML for printing
589
+ const printWindow = window.open('', '_blank');
590
+
591
+ if (printWindow) {
592
+ // Create a document with RTL support for Arabic
593
+ printWindow.document.write(`
594
+ <!DOCTYPE html>
595
+ <html dir="rtl">
596
+ <head>
597
+ <meta charset="UTF-8">
598
+ <title>${translatedFileName}</title>
599
+ <style>
600
+ @page { margin: 1.5cm; }
601
+ body {
602
+ font-family: 'Arial', 'Segoe UI', 'Tahoma', sans-serif;
603
+ line-height: 1.5;
604
+ direction: rtl;
605
+ text-align: right;
606
+ padding: 20px;
607
+ font-size: 14pt;
608
+ }
609
+ .content {
610
+ white-space: pre-wrap;
611
+ }
612
+ </style>
613
+ </head>
614
+ <body>
615
+ <div class="content">${content}</div>
616
+ <script>
617
+ // Auto-print and close when done
618
+ window.onload = function() {
619
+ setTimeout(function() {
620
+ window.print();
621
+ // Wait for print dialog to close
622
+ setTimeout(function() {
623
+ window.close();
624
+ }, 500);
625
+ }, 500);
626
+ };
627
+ </script>
628
+ </body>
629
+ </html>
630
+ `);
631
+
632
+ // Show a notification
633
+ showNotification('Print dialog will open. Select "Save as PDF" option to download your translation.');
634
+ return;
635
+ } else {
636
+ // Fallback if popup is blocked
637
+ showError('Popup blocked. Please allow popups and try again.');
638
+ }
639
+ }
640
 
 
641
  if (extension === '.txt') {
642
+ // Direct browser download for text files
643
+ const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
644
  const url = URL.createObjectURL(blob);
645
+ triggerDownload(url, translatedFileName);
 
 
 
 
 
 
 
 
 
646
  } else {
647
+ // Server-side processing for complex formats
648
  fetch('/download/translated-document', {
649
  method: 'POST',
650
  headers: {
 
653
  body: JSON.stringify({
654
  content: content,
655
  filename: translatedFileName,
656
+ original_type: fileType
657
  }),
658
  })
659
  .then(response => {
660
  if (!response.ok) {
661
+ throw new Error(`HTTP error! Status: ${response.status}`);
662
  }
663
  return response.blob();
664
  })
665
  .then(blob => {
666
+ const url = URL.createObjectURL(blob);
667
+ triggerDownload(url, translatedFileName);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
668
  })
669
  .catch(error => {
670
+ showError(`Error downloading file: ${error.message}`);
 
671
  });
672
  }
673
  }
674
 
675
+ function triggerDownload(url, filename) {
676
+ const a = document.createElement('a');
677
+ a.href = url;
678
+ a.download = filename;
679
+ document.body.appendChild(a);
680
+ a.click();
681
+ document.body.removeChild(a);
682
+ URL.revokeObjectURL(url);
683
+ }
684
+
685
  // Helper function to get language name from code
686
  function getLanguageName(code) {
687
  // Hard-coded mapping for common languages