Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (141 loc) · 4.75 KB
/
Copy pathindex.js
File metadata and controls
170 lines (141 loc) · 4.75 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
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
import OpenAI from 'openai';
// Load API key from environment variable or prompt user
const getApiKey = async () => {
// Option 1: Try to load from environment variable (if using Node.js backend)
if (typeof process !== 'undefined' && process.env.OPENAI_API_KEY) {
return process.env.OPENAI_API_KEY;
}
// Option 2: For client-side, you'll need to handle this differently
// You could fetch from your backend or prompt the user
return prompt('Please enter your OpenAI API key:');
};
// 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';
}
async function translate(text, language) {
const myMessages = [
{
role: 'system',
content: `You are a language translator. Given text, translate the text into ${language}`
},
{
role: 'user',
content: text
}
]
try {
const opai = new OpenAI({
apiKey: await getApiKey(),
dangerouslyAllowBrowser: true
})
const response = await opai.chat.completions.create({
model: 'gpt-4',
messages: myMessages
})
console.log(response)
return response.choices[0].message.content
} catch (e) {
console.log(e.message)
}
}
// Show translation result
async function showTranslationResult() {
const inputValue = inputText.value.trim();
const selectedLanguage = getSelectedLanguage();
if (!inputValue) {
alert('Please enter some text to translate');
return;
}
// Set the original text
originalText.textContent = inputValue;
// Show loading message
translatedText.textContent = 'Translating...';
// Switch screens
inputScreen.classList.add('hidden');
resultScreen.classList.remove('hidden');
// Get translation and update
const translation = await translate(inputValue, selectedLanguage);
translatedText.textContent = translation;
}
// 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();
}
});