|
|
|
window.onload = function() { |
|
console.log('Window fully loaded, initializing translation app'); |
|
|
|
|
|
const textTranslationForm = document.querySelector('#text-translation-form'); |
|
const docTranslationForm = document.querySelector('#doc-translation-form'); |
|
|
|
|
|
if (textTranslationForm) { |
|
console.log('Text translation form found'); |
|
|
|
|
|
textTranslationForm.addEventListener('submit', function(event) { |
|
event.preventDefault(); |
|
console.log('Text translation form submitted'); |
|
|
|
|
|
document.querySelectorAll('#text-result, #error-message, #debug-info').forEach(el => { |
|
if (el) el.style.display = 'none'; |
|
}); |
|
|
|
|
|
const textInput = this.querySelector('#text-input'); |
|
const sourceLang = this.querySelector('#source-lang-text'); |
|
const targetLang = this.querySelector('#target-lang-text'); |
|
const loadingElement = document.querySelector('#text-loading'); |
|
const debugElement = document.querySelector('#debug-info'); |
|
|
|
|
|
console.log('Elements found:', { |
|
textInput: !!textInput, |
|
sourceLang: !!sourceLang, |
|
targetLang: !!targetLang |
|
}); |
|
|
|
|
|
if (debugElement) { |
|
debugElement.textContent = `Form elements found: |
|
text-input: ${!!textInput} |
|
source-lang-text: ${!!sourceLang} |
|
target-lang-text: ${!!targetLang}`; |
|
debugElement.style.display = 'block'; |
|
} |
|
|
|
|
|
if (!textInput || !sourceLang || !targetLang) { |
|
showError('Form elements not found. Please check the HTML structure.'); |
|
return; |
|
} |
|
|
|
|
|
const text = textInput.value ? textInput.value.trim() : ''; |
|
if (!text) { |
|
showError('Please enter text to translate'); |
|
return; |
|
} |
|
|
|
|
|
const sourceLangValue = sourceLang.value; |
|
const targetLangValue = targetLang.value; |
|
|
|
|
|
if (loadingElement) loadingElement.style.display = 'block'; |
|
|
|
|
|
const payload = { |
|
text: text, |
|
source_lang: sourceLangValue, |
|
target_lang: targetLangValue |
|
}; |
|
|
|
console.log('Sending payload:', payload); |
|
|
|
|
|
fetch('/translate/text', { |
|
method: 'POST', |
|
headers: { 'Content-Type': 'application/json' }, |
|
body: JSON.stringify(payload) |
|
}) |
|
.then(function(response) { |
|
if (!response.ok) throw new Error('Server error: ' + response.status); |
|
return response.text(); |
|
}) |
|
.then(function(responseText) { |
|
console.log('Response received:', responseText); |
|
|
|
|
|
let data; |
|
try { |
|
data = JSON.parse(responseText); |
|
} catch (error) { |
|
throw new Error('Invalid response from server'); |
|
} |
|
|
|
|
|
if (!data.translated_text) { |
|
throw new Error('No translation returned'); |
|
} |
|
|
|
|
|
const resultBox = document.querySelector('#text-result'); |
|
const outputElement = document.querySelector('#text-output'); |
|
|
|
if (resultBox && outputElement) { |
|
outputElement.textContent = data.translated_text; |
|
resultBox.style.display = 'block'; |
|
} |
|
}) |
|
.catch(function(error) { |
|
showError(error.message || 'Translation failed'); |
|
console.error('Error:', error); |
|
}) |
|
.finally(function() { |
|
if (loadingElement) loadingElement.style.display = 'none'; |
|
}); |
|
}); |
|
} else { |
|
console.error('Text translation form not found in the document!'); |
|
} |
|
|
|
|
|
if (docTranslationForm) { |
|
console.log('Document translation form found'); |
|
docTranslationForm.addEventListener('submit', function(event) { |
|
event.preventDefault(); |
|
console.log('Document translation form submitted'); |
|
|
|
|
|
document.querySelectorAll('#doc-result, #error-message').forEach(el => { |
|
if (el) el.style.display = 'none'; |
|
}); |
|
|
|
|
|
const fileInput = docTranslationForm.querySelector('#doc-input'); |
|
const loadingIndicator = document.querySelector('#doc-loading'); |
|
|
|
if (!fileInput || !fileInput.files || fileInput.files.length === 0) { |
|
showError('Please select a document to upload'); |
|
return; |
|
} |
|
|
|
|
|
if (loadingIndicator) loadingIndicator.style.display = 'block'; |
|
|
|
|
|
const formData = new FormData(docTranslationForm); |
|
|
|
|
|
fetch('/translate/document', { |
|
method: 'POST', |
|
body: formData |
|
}) |
|
.then(function(response) { |
|
if (!response.ok) { |
|
throw new Error(`Server returned ${response.status}`); |
|
} |
|
return response.json(); |
|
}) |
|
.then(function(data) { |
|
if (!data.translated_text) { |
|
throw new Error('No translation returned'); |
|
} |
|
|
|
|
|
const resultBox = document.querySelector('#doc-result'); |
|
const outputEl = document.querySelector('#doc-output'); |
|
const filenameEl = document.querySelector('#doc-filename'); |
|
const sourceLangEl = document.querySelector('#doc-source-lang'); |
|
|
|
if (resultBox && outputEl) { |
|
if (filenameEl) filenameEl.textContent = data.original_filename || 'N/A'; |
|
if (sourceLangEl) sourceLangEl.textContent = data.detected_source_lang || 'N/A'; |
|
outputEl.textContent = data.translated_text; |
|
resultBox.style.display = 'block'; |
|
} |
|
}) |
|
.catch(function(error) { |
|
showError(error.message); |
|
}) |
|
.finally(function() { |
|
if (loadingIndicator) { |
|
loadingIndicator.style.display = 'none'; |
|
} |
|
}); |
|
}); |
|
} else { |
|
console.error('Document translation form not found in the document!'); |
|
} |
|
|
|
|
|
function showError(message) { |
|
const errorDiv = document.querySelector('#error-message'); |
|
if (errorDiv) { |
|
errorDiv.textContent = 'Error: ' + message; |
|
errorDiv.style.display = 'block'; |
|
} |
|
console.error('Error:', message); |
|
} |
|
}; |
|
|