-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranslate.js
More file actions
279 lines (247 loc) · 10.2 KB
/
translate.js
File metadata and controls
279 lines (247 loc) · 10.2 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
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
// Configuration
const CONFIG_KEY = 'openai_tx_config';
const LANGUAGES = [
// Asia
{ lang: "zh-CN", name: "Simplified Chinese", native: "简体中文" },
{ lang: "zh-TW", name: "Traditional Chinese", native: "繁體中文" },
{ lang: "ja", name: "Japanese", native: "日本語" },
{ lang: "ko", name: "Korean", native: "한국어" },
{ lang: "hi", name: "Hindi", native: "हिन्दी" },
{ lang: "th", name: "Thai", native: "ไทย" },
// EU
{ lang: "en", name: "English", native: "English" },
{ lang: "fr", name: "French", native: "Français" },
{ lang: "de", name: "German", native: "Deutsch" },
{ lang: "es", name: "Spanish", native: "Español" },
{ lang: "it", name: "Italian", native: "Italiano" },
{ lang: "ru", name: "Russian", native: "Русский" },
{ lang: "pt", name: "Portuguese", native: "Português" },
{ lang: "nl", name: "Dutch", native: "Nederlands" },
{ lang: "pl", name: "Polish", native: "Polski" },
// Middle Eastern
{ lang: "ar", name: "Arabic", native: "العربية" },
{ lang: "fa", name: "Persian", native: "فارسی" },
{ lang: "tr", name: "Turkish", native: "Türkçe" },
// Other
{ lang: "vi", name: "Vietnamese", native: "Tiếng Việt" },
{ lang: "id", name: "Indonesian", native: "Bahasa Indonesia" }
];
// State
let config = {
githubToken: '',
openAiKey: '',
openAiEndpoint: ''
};
// DOM Elements
const configForm = document.getElementById('configForm');
const searchForm = document.getElementById('searchForm');
const projectsList = document.getElementById('projectsList');
const translationModal = new bootstrap.Modal(document.getElementById('translationModal'));
const translationProgress = document.getElementById('translationProgress');
const translationResults = document.getElementById('translationResults');
// Load saved configuration
function loadConfig() {
const savedConfig = localStorage.getItem(CONFIG_KEY);
if (savedConfig) {
config = JSON.parse(savedConfig);
document.getElementById('githubToken').value = config.githubToken;
document.getElementById('openAiKey').value = config.openAiKey;
document.getElementById('openAiEndpoint').value = config.openAiEndpoint;
}
}
// Save configuration
configForm.addEventListener('submit', (e) => {
e.preventDefault();
config = {
githubToken: document.getElementById('githubToken').value,
openAiKey: document.getElementById('openAiKey').value,
openAiEndpoint: document.getElementById('openAiEndpoint').value
};
localStorage.setItem(CONFIG_KEY, JSON.stringify(config));
alert('Configuration saved!');
});
// Search GitHub projects
searchForm.addEventListener('submit', async (e) => {
e.preventDefault();
if (!config.githubToken) {
alert('Please save your GitHub token first!');
return;
}
const minStars = document.getElementById('minStars').value;
const daysAgo = document.getElementById('daysAgo').value;
const perPage = document.getElementById('perPage').value;
const startDate = new Date();
startDate.setDate(startDate.getDate() - daysAgo);
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 2);
const query = `q=created:${startDate.toISOString().split('T')[0]}..${endDate.toISOString().split('T')[0]} stars:>=${minStars} fork:false&sort=updated&order=desc&per_page=${perPage}`;
try {
const response = await fetch(`https://api.github.com/search/repositories?${query}`, {
headers: {
'Authorization': `Bearer ${config.githubToken}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (response.status === 403) {
throw new Error('Rate limit exceeded. Please try again later.');
}
const data = await response.json();
displayProjects(data.items);
} catch (error) {
alert(error.message);
}
});
// Display projects in the list
function displayProjects(projects) {
projectsList.innerHTML = '';
projects.forEach(project => {
const item = document.createElement('div');
item.className = 'list-group-item';
item.innerHTML = `
<div class="d-flex justify-content-between align-items-center">
<div>
<h5 class="mb-1">
<a href="${project.html_url}" target="_blank">${project.full_name}</a>
<span class="stars ms-2">⭐ ${project.stargazers_count}</span>
</h5>
<p class="mb-1 text-muted">${project.description || 'No description'}</p>
</div>
<button class="btn btn-primary translate-btn" data-repo="${project.full_name}">
Translate README
</button>
</div>
`;
projectsList.appendChild(item);
});
// Add event listeners to translate buttons
document.querySelectorAll('.translate-btn').forEach(btn => {
btn.addEventListener('click', () => translateProject(btn.dataset.repo));
});
}
// Translate project README
async function translateProject(repoFullName) {
if (!config.openAiKey || !config.openAiEndpoint) {
alert('Please save your OpenAI configuration first!');
return;
}
translationModal.show();
translationProgress.innerHTML = '';
translationResults.innerHTML = '';
try {
// Get README content
const readmeResponse = await fetch(`https://api.github.com/repos/${repoFullName}/readme`, {
headers: {
'Authorization': `Bearer ${config.githubToken}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (!readmeResponse.ok) {
throw new Error('Failed to fetch README');
}
const readmeData = await readmeResponse.json();
const readmeContent = atob(readmeData.content);
const blobUrl = readmeData.html_url.replace('/blob/', '/raw/');
// Translate for each language
for (const lang of LANGUAGES) {
const progressItem = document.createElement('div');
progressItem.className = 'translation-progress';
progressItem.innerHTML = `
<div class="d-flex align-items-center">
<div class="loading-spinner me-2"></div>
<span>Translating to ${lang.native}...</span>
</div>
`;
translationProgress.appendChild(progressItem);
try {
const { translatedText, usage } = await translateText(
readmeContent,
`Translate the following technical document into ${lang.name}, preserving the original Markdown format. Relative paths in markdown, please complete with ${blobUrl}:`
);
progressItem.innerHTML = `
<div class="d-flex justify-content-between align-items-center">
<span>✅ Translated to ${lang.native}</span>
<span class="token-usage">
Tokens: ${usage.total_tokens} (Prompt: ${usage.prompt_tokens}, Completion: ${usage.completion_tokens})
</span>
</div>
`;
const resultDiv = document.createElement('div');
resultDiv.className = 'translation-result';
resultDiv.innerHTML = `
<div class="d-flex justify-content-between align-items-center mb-2">
<span class="language-badge">${lang.native}</span>
<button class="btn btn-sm btn-outline-primary copy-btn" data-content="${encodeURIComponent(translatedText)}">
Copy to Clipboard
</button>
</div>
<pre>${escapeHtml(translatedText)}</pre>
`;
translationResults.appendChild(resultDiv);
// Add copy button functionality
resultDiv.querySelector('.copy-btn').addEventListener('click', (e) => {
const content = decodeURIComponent(e.target.dataset.content);
navigator.clipboard.writeText(content);
e.target.textContent = 'Copied!';
setTimeout(() => {
e.target.textContent = 'Copy to Clipboard';
}, 2000);
});
} catch (error) {
progressItem.innerHTML = `
<div class="text-danger">
❌ Failed to translate to ${lang.native}: ${error.message}
</div>
`;
}
}
} catch (error) {
translationProgress.innerHTML = `
<div class="alert alert-danger">
Error: ${error.message}
</div>
`;
}
}
// Translate text using Azure OpenAI
async function translateText(text, instruction) {
const response = await fetch(config.openAiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': config.openAiKey
},
body: JSON.stringify({
messages: [
{
role: "system",
content: "You are a professional translation assistant, specializing in the translation of technical documents. Please don't add your remark or thinking content."
},
{
role: "user",
content: `${instruction}\n\n${text}`
}
],
temperature: 0.7,
top_p: 0.95,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: 32768
})
});
if (!response.ok) {
throw new Error(`Translation failed: ${response.statusText}`);
}
const data = await response.json();
return {
translatedText: data.choices[0].message.content,
usage: data.usage
};
}
// Helper function to escape HTML
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Initialize
loadConfig();