-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (100 loc) · 3.45 KB
/
index.js
File metadata and controls
114 lines (100 loc) · 3.45 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Translation mappings
const translations = {
french: {
'how are you?': 'Comment allez-vous?',
'hello': 'Bonjour',
'goodbye': 'Au revoir',
'thank you': 'Merci',
'please': 'S\'il vous plaît',
'yes': 'Oui',
'no': 'Non',
'good morning': 'Bonjour',
'good evening': 'Bonsoir',
'i love you': 'Je t\'aime'
},
spanish: {
'how are you?': '¿Cómo estás?',
'hello': 'Hola',
'goodbye': 'Adiós',
'thank you': 'Gracias',
'please': 'Por favor',
'yes': 'Sí',
'no': 'No',
'good morning': 'Buenos días',
'good evening': 'Buenas noches',
'i love you': 'Te amo'
},
japanese: {
'how are you?': '元気ですか?',
'hello': 'こんにちは',
'goodbye': 'さようなら',
'thank you': 'ありがとう',
'please': 'お願いします',
'yes': 'はい',
'no': 'いいえ',
'good morning': 'おはよう',
'good evening': 'こんばんは',
'i love you': '愛してる'
}
};
// DOM elements
const inputScreen = document.getElementById('input-screen');
const resultScreen = document.getElementById('result-screen');
const translateBtn = document.getElementById('translate-btn');
const startOverBtn = document.getElementById('start-over-btn');
const inputText = document.getElementById('input-text');
const originalText = document.getElementById('original-text');
const translatedText = document.getElementById('translated-text');
// Get selected language
function getSelectedLanguage() {
const selectedRadio = document.querySelector('input[name="language"]:checked');
return selectedRadio ? selectedRadio.value : 'french';
}
// Translate text
function translateText(text, targetLanguage) {
const normalizedText = text.toLowerCase().trim();
const languageTranslations = translations[targetLanguage];
// Check for exact match first
if (languageTranslations[normalizedText]) {
return languageTranslations[normalizedText];
}
// If no translation found, return a default message
const defaultMessages = {
french: 'Traduction non disponible',
spanish: 'Traducción no disponible',
japanese: '翻訳が利用できません'
};
return defaultMessages[targetLanguage] || 'Translation not available';
}
// Show translation result
function showTranslationResult() {
const inputValue = inputText.value.trim();
const selectedLanguage = getSelectedLanguage();
if (!inputValue) {
alert('Please enter some text to translate');
return;
}
// Set the original and translated text
originalText.textContent = inputValue;
translatedText.textContent = translateText(inputValue, selectedLanguage);
// Switch screens
inputScreen.classList.add('hidden');
resultScreen.classList.remove('hidden');
}
// Show input screen
function showInputScreen() {
resultScreen.classList.add('hidden');
inputScreen.classList.remove('hidden');
// Clear input
inputText.value = '';
}
// Event listeners
translateBtn.addEventListener('click', showTranslationResult);
startOverBtn.addEventListener('click', showInputScreen);
// Handle enter key in textarea
inputText.addEventListener('keydown', function(event) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
showTranslationResult();
}
});