File size: 12,852 Bytes
4ca439a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Translator</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #F26522;  /* Orange */
            --primary-hover: #E05B1F;  /* Slightly darker orange for hover */
            --bg-color: #FFFFFF;       /* White */
            --text-color: #78797D;    /* Grey */
            --border-color: #E5E7EB;  /* Light grey for borders */
            --success-color: #F26522; /* Use primary color for success */
            --error-color: #DC2626;   /* Red for errors */
            --container-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        [data-theme='dark'] {
            --bg-color: #1A1A1A;
            --text-color: #E5E5E5;
            --border-color: #333333;
            --container-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2), 0 2px 4px -1px rgba(0, 0, 0, 0.1);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            transition: background-color 0.3s ease, color 0.3s ease;
        }

        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            line-height: 1.5;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background-color: var(--bg-color);
            padding: 2.5rem;
            border-radius: 1.5rem;
            box-shadow: var(--container-shadow);
            width: 100%;
            max-width: 800px;
            transition: all 0.3s ease;
        }

        .container:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 8px -1px rgba(0, 0, 0, 0.1), 0 4px 6px -1px rgba(0, 0, 0, 0.06);
        }

        h1 {
            font-size: 2.25rem;
            font-weight: 600;
            color: var(--text-color);
            margin-bottom: 2rem;
            text-align: center;
            letter-spacing: -0.5px;
        }

        .form-group {
            margin-bottom: 2rem;
        }

        .input-container, .output-container {
            position: relative;
            margin-bottom: 1.5rem;
        }

        .label {
            display: block;
            font-size: 0.95rem;
            font-weight: 500;
            color: var(--text-color);
            margin-bottom: 0.75rem;
            letter-spacing: 0.2px;
        }

        textarea {
            width: 100%;
            padding: 1rem;
            border: 2px solid var(--border-color);
            border-radius: 0.75rem;
            font-size: 1rem;
            font-family: inherit;
            min-height: 120px;
            resize: vertical;
            transition: all 0.2s ease;
            background-color: var(--bg-color);
            color: var(--text-color);
        }

        textarea:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(242, 101, 34, 0.1);
        }

        .theme-toggle {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 12px 24px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.95rem;
            font-weight: 500;
            transition: all 0.3s ease;
            z-index: 1000;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .theme-toggle:hover {
            background-color: var(--primary-hover);
            transform: translateY(-2px);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .theme-toggle:active {
            transform: translateY(0);
        }

        .button-container {
            display: flex;
            gap: 1rem;
            margin-top: 1rem;
        }

        .translate-btn, .speak-btn {
            padding: 0.75rem 1.5rem;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.95rem;
            font-weight: 500;
            transition: all 0.3s ease;
            flex: 1;
        }

        .translate-btn:hover, .speak-btn:hover {
            background-color: var(--primary-hover);
            transform: translateY(-2px);
        }

        .translate-btn:active, .speak-btn:active {
            transform: translateY(0);
        }

        .char-count {
            position: absolute;
            bottom: -1.5rem;
            right: 0;
            font-size: 0.85rem;
            color: var(--text-color);
            opacity: 0.8;
        }

        .chat-history {
            margin-top: 2rem;
            padding: 1.5rem;
            border-radius: 1rem;
            background-color: var(--bg-color);
            border: 2px solid var(--border-color);
        }

        .chat-history h2 {
            font-size: 1.25rem;
            margin-bottom: 1rem;
            color: var(--text-color);
        }

        .chat-item {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 0.75rem;
            background-color: rgba(242, 101, 34, 0.1);
            border: 1px solid var(--border-color);
        }

        .chat-item:last-child {
            margin-bottom: 0;
        }

        .translation-bubble {
            margin-top: 1.5rem;
            padding: 1rem;
            border-radius: 1rem;
            background-color: var(--bg-color);
            border: 2px solid var(--border-color);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            margin-left: auto;
            margin-right: auto;
        }

        .translation-bubble h2 {
            font-size: 1.25rem;
            margin-bottom: 0.5rem;
            color: var(--text-color);
        }

        .translation-bubble p {
            font-size: 1rem;
            color: var(--text-color);
            line-height: 1.5;
        }

        @media (max-width: 640px) {
            .container {
                padding: 1.5rem;
            }

            h1 {
                font-size: 1.75rem;
            }

            .button-container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body data-theme="light">
    <button class="theme-toggle">Switch to Dark Mode</button>
    <div class="container">
        <h1>Translator</h1>
        <div class="form-group">
            <div class="input-container">
                <label class="label" for="inputText">Original</label>
                <textarea 
                    id="inputText" 
                    placeholder="Enter text to translate..."
                    maxlength="500"
                    oninput="updateCharCount(this)"
                ></textarea>
                <div class="char-count">0/500</div>
            </div>
            <div class="direction-container">
                <label class="label" for="direction">Translation Direction</label>
                <select id="direction">
                    <option value="en-fi">English to Finnish</option>
                    <option value="fi-en">Finnish to English</option>
                </select>
            </div>
        </div>
        <div class="button-container">
            <button class="translate-btn" onclick="translateText()">
                Translate
            </button>
            <button class="speak-btn" onclick="speakText()">
                Speak Translation
            </button>
        </div>
        <div class="error" id="errorMsg"></div>
        <div class="translation-bubble">
            <h2>Translation</h2>
            <p id="result"></p>
        </div>

        <div class="chat-history" id="chatHistory">
            <h2>Chat History</h2>
            <ul class="chat-list" id="chatList">
            </ul>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const themeToggleButton = document.querySelector('.theme-toggle');
            if (themeToggleButton) {
                console.log('Theme toggle button found');
                themeToggleButton.addEventListener('click', () => {
                    console.log('Toggle theme button clicked');
                    const body = document.body;
                    const currentTheme = body.getAttribute('data-theme');
                    console.log('Current theme:', currentTheme);
                    const newTheme = currentTheme === 'light' ? 'dark' : 'light';
                    console.log('Switching to theme:', newTheme);
                    body.setAttribute('data-theme', newTheme);
                    themeToggleButton.textContent = `Switch to ${newTheme === 'light' ? 'Dark' : 'Light'} Mode`;
                });
            } else {
                console.error('Theme toggle button not found');
            }
        });

        function updateCharCount(textarea) {
            const maxLength = textarea.getAttribute('maxlength');
            const currentLength = textarea.value.length;
            const charCountElement = textarea.parentElement.querySelector('.char-count');
            charCountElement.textContent = `${currentLength}/${maxLength}`;
        }

        async function translateText() {
            const inputText = document.getElementById('inputText').value;
            const direction = document.getElementById('direction').value;
            const translateBtn = document.querySelector('.translate-btn');
            const loading = document.querySelector('.loading');
            const result = document.getElementById('result');
            const resultContainer = document.querySelector('.translation-bubble');
            const errorMsg = document.getElementById('errorMsg');

            if (!inputText.trim()) {
                errorMsg.textContent = 'Please enter some text to translate';
                errorMsg.style.display = 'block';
                return;
            }

            // Reset states
            errorMsg.style.display = 'none';
            translateBtn.disabled = true;
            result.textContent = '';
            resultContainer.classList.remove('show');

            try {
                const response = await fetch('/translate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ text: inputText, direction: direction })
                });

                const data = await response.json();
                
                if (response.ok) {
                    result.textContent = data.translation;
                    resultContainer.classList.add('show');
                    // Update chat history
                    updateChatHistory(inputText, data.translation);
                } else {
                    errorMsg.textContent = data.error || 'Failed to translate';
                    errorMsg.style.display = 'block';
                }
            } catch (error) {
                console.error('Translation error:', error);
                errorMsg.textContent = 'Failed to connect to the server';
                errorMsg.style.display = 'block';
            } finally {
                translateBtn.disabled = false;
            }
        }

        function updateChatHistory(input, output) {
            const chatList = document.getElementById('chatList');
            const messageLi = document.createElement('li');
            messageLi.classList.add('chat-item');

            const originalDiv = document.createElement('div');
            originalDiv.classList.add('chat-original');
            originalDiv.textContent = `Original: ${input}`;

            const translationDiv = document.createElement('div');
            translationDiv.classList.add('chat-translation');
            translationDiv.textContent = `Translation: ${output}`;

            messageLi.appendChild(originalDiv);
            messageLi.appendChild(translationDiv);

            chatList.appendChild(messageLi);
            chatList.scrollTop = chatList.scrollHeight;
        }

        function speakText() {
            const text = document.getElementById('result').textContent;
            if (!text) return;

            const utterance = new SpeechSynthesisUtterance(text);
            window.speechSynthesis.speak(utterance);
        }
    </script>
</body>
</html>