-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (65 loc) · 3.15 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*If you use this code, with or without modifications, please credit the creator kanata-05
https://www.github.com/kanata-05/language-converter
This code is licensed under the CC-BY 3.0 License. */
document.getElementById('btnTranslate').addEventListener('click', function () {
var sl = document.getElementById('slang').value;
var tl = document.getElementById('tlang').value;
var text = document.getElementById('txtSource').value;
var url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${encodeURIComponent(text)}`;
fetch(url)
.then(response => response.json())
.then(data => {
var translation = data[0][0][0];
document.getElementById('txtTarget').value = translation;
})
.catch(error => console.error('Error:', error));
// Function to save translation as text file
document.getElementById('btnSaveText').addEventListener('click', function () {
var translation = document.getElementById('txtTarget').value;
var blob = new Blob([translation], { type: 'text/plain' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'translation.txt';
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
});
// Function to save translation as Word document
document.getElementById('btnSaveDoc').addEventListener('click', function () {
var textToWrite = document.getElementById('txtTarget').value;
var blob = new Blob(['\ufeff', textToWrite], {
type: 'application/msword'
});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'translation.doc';
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
});
// Function to print translation
document.getElementById('btnPrint').addEventListener('click', function () {
var printContents = document.getElementById('txtTarget').value;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
});
// Function to update character and word counts
function updateCharacterCount(sourceText, translatedText) {
var sourceCharCount = sourceText.length;
var sourceWordCount = sourceText.trim().split(/\s+/).length;
var translationCharCount = translatedText.length;
var translationWordCount = translatedText.trim().split(/\s+/).length;
document.getElementById('charCount').textContent = `${sourceCharCount} (Source) / ${translationCharCount} (Translation)`;
document.getElementById('wordCount').textContent = `${sourceWordCount} (Source) / ${translationWordCount} (Translation)`;
}
});