Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
385 lines (316 loc) · 16.5 KB
/
Copy pathscript.js
File metadata and controls
385 lines (316 loc) · 16.5 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
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
let englishData = {};
let existingTranslationData = {};
let translations = {};
let originalLocale = 'en';
let targetLocale = '';
let currentFilter = 'all';
document.getElementById('englishFile').addEventListener('change', handleEnglishFileUpload);
document.getElementById('translationFile').addEventListener('change', handleTranslationFileUpload);
document.getElementById('targetLanguage').addEventListener('input', updateTargetLanguage);
// Filter button functionality
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
e.target.classList.add('active');
currentFilter = e.target.dataset.filter;
applyFilter();
});
});
function toggleNewItemsFilterBtn() {
const btn = document.getElementById('newItemsFilterBtn');
const englishLoaded = Object.keys(englishData).length > 0;
const translationLoaded = Object.keys(existingTranslationData).length > 0;
btn.style.display = (englishLoaded && translationLoaded) ? '' : 'none';
}
function handleEnglishFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
console.log('English file selected:', file.name);
const reader = new FileReader();
reader.onload = function(e) {
try {
englishData = JSON.parse(e.target.result);
originalLocale = englishData['@@locale'] || 'en';
document.getElementById('englishFileName').textContent = file.name;
document.getElementById('englishStatus').classList.add('loaded');
document.getElementById('englishStatus').classList.remove('not-loaded');
document.getElementById('originalLocale').textContent = originalLocale;
updateFileInfo();
if (Object.keys(englishData).length > 0) {
processData();
}
} catch (error) {
console.error('Error parsing English file:', error);
alert('Error parsing English file. Please make sure it\'s a valid JSON file.');
}
};
reader.onerror = function(error) {
console.error('Error reading English file:', error);
alert('Error reading English file. Please try again.');
};
reader.readAsText(file);
}
function handleTranslationFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
console.log('Translation file selected:', file.name);
const reader = new FileReader();
reader.onload = function(e) {
try {
existingTranslationData = JSON.parse(e.target.result);
targetLocale = existingTranslationData['@@locale'] || '';
document.getElementById('translationFileName').textContent = file.name;
document.getElementById('translationStatus').classList.add('loaded');
document.getElementById('translationStatus').classList.remove('not-loaded');
document.getElementById('targetLocaleDisplay').textContent = targetLocale || 'Not specified';
if (targetLocale) {
document.getElementById('targetLanguage').value = targetLocale;
}
updateFileInfo();
if (Object.keys(englishData).length > 0) {
processData();
}
} catch (error) {
console.error('Error parsing translation file:', error);
alert('Error parsing translation file. Please make sure it\'s a valid JSON file.');
}
};
reader.onerror = function(error) {
console.error('Error reading translation file:', error);
alert('Error reading translation file. Please try again.');
};
reader.readAsText(file);
}
function updateFileInfo() {
document.getElementById('fileInfo').classList.add('show');
}
function processData() {
if (Object.keys(englishData).length === 0) return;
console.log('Processing data...');
const translatableItems = {};
for (const [key, value] of Object.entries(englishData)) {
if (!key.startsWith('@') && key !== '@@locale' && typeof value === 'string') {
translatableItems[key] = {
text: value,
description: englishData[`@${key}`]?.description || '',
placeholders: englishData[`@${key}`]?.placeholders || {},
isNew: !existingTranslationData.hasOwnProperty(key),
existingTranslation: existingTranslationData[key] || ''
};
}
}
// Pre-fill translations from existing file
translations = {};
let preFilledCount = 0;
for (const [key, item] of Object.entries(translatableItems)) {
if (item.existingTranslation) {
translations[key] = item.existingTranslation;
preFilledCount++;
}
}
console.log('Translatable items:', translatableItems);
console.log('Pre-filled translations:', preFilledCount);
document.getElementById('itemCount').textContent = Object.keys(translatableItems).length;
document.getElementById('preFilledCount').textContent = preFilledCount;
renderTranslationItems(translatableItems);
updateStats();
document.getElementById('languageSection').classList.remove('hidden');
document.getElementById('exportSection').classList.remove('hidden');
toggleNewItemsFilterBtn();
}
function renderTranslationItems(items) {
const container = document.getElementById('translationContainer');
container.innerHTML = '';
for (const [key, item] of Object.entries(items)) {
const itemDiv = document.createElement('div');
itemDiv.className = `translation-item ${item.isNew ? 'new' : 'existing'}`;
itemDiv.dataset.key = key;
itemDiv.dataset.type = item.isNew ? 'new' : 'existing';
const placeholders = extractPlaceholders(item.text);
const placeholderInfo = placeholders.length > 0 ?
`<div class="placeholder-info">
<strong>Required placeholders:</strong> ${placeholders.join(', ')}
</div>` : '';
const showNewBadge = Object.keys(existingTranslationData).length > 0;
const statusBadges = `
<div class="status-badges">
<div class="status-badge ${item.existingTranslation ? 'completed' : 'pending'}">${item.existingTranslation ? 'Completed' : 'Pending'}</div>
${showNewBadge ? `<div class="status-badge ${item.isNew ? 'new' : 'existing'}">${item.isNew ? 'New' : 'Existing'}</div>` : ''}
</div>
`;
itemDiv.innerHTML = `
<div class="item-header">
<div class="key-name">${key}</div>
${statusBadges}
</div>
<div class="original-text">
<div class="original-label">Original (${originalLocale})</div>
<div>${item.text}</div>
</div>
<textarea class="translation-input"
placeholder="Enter translation..."
data-key="${key}"
oninput="handleTranslationInput(this)">${item.existingTranslation}</textarea>
${placeholderInfo}
${item.description ? `<div class="description">Description: ${item.description}</div>` : ''}
<div class="error-message hidden"></div>
`;
container.appendChild(itemDiv);
if (item.existingTranslation) {
updateItemStatus(itemDiv, item.existingTranslation, key);
}
}
console.log('Rendered', Object.keys(items).length, 'translation items');
applyFilter();
}
function extractPlaceholders(text) {
const matches = text.match(/\{[^}]+\}/g);
return matches || [];
}
function handleTranslationInput(input) {
const key = input.dataset.key;
const value = input.value.trim();
const itemDiv = input.closest('.translation-item');
updateItemStatus(itemDiv, value, key);
updateStats();
}
function updateItemStatus(itemDiv, value, key) {
const statusBadge = itemDiv.querySelector('.status-badge');
const errorDiv = itemDiv.querySelector('.error-message');
const input = itemDiv.querySelector('.translation-input');
const originalText = englishData[key];
const originalPlaceholders = extractPlaceholders(originalText);
const translationPlaceholders = extractPlaceholders(value);
let isValid = true;
let errorMessage = '';
if (originalPlaceholders.length > 0) {
const missingPlaceholders = originalPlaceholders.filter(p => !translationPlaceholders.includes(p));
const extraPlaceholders = translationPlaceholders.filter(p => !originalPlaceholders.includes(p));
if (missingPlaceholders.length > 0) {
isValid = false;
errorMessage = `Missing placeholders: ${missingPlaceholders.join(', ')}`;
}
if (extraPlaceholders.length > 0) {
isValid = false;
errorMessage += (errorMessage ? ' | ' : '') + `Extra placeholders: ${extraPlaceholders.join(', ')}`;
}
}
const isExisting = itemDiv.dataset.type === 'existing';
const baseClass = `translation-item ${isExisting ? 'existing' : 'new'}`;
if (value === '') {
itemDiv.className = baseClass;
statusBadge.className = 'status-badge pending';
statusBadge.textContent = 'Pending';
input.classList.remove('error');
errorDiv.classList.add('hidden');
delete translations[key];
} else if (!isValid) {
itemDiv.className = baseClass + ' error';
statusBadge.className = 'status-badge error';
statusBadge.textContent = 'Error';
input.classList.add('error');
errorDiv.textContent = errorMessage;
errorDiv.classList.remove('hidden');
delete translations[key];
} else {
itemDiv.className = baseClass + ' completed';
statusBadge.className = 'status-badge completed';
statusBadge.textContent = 'Completed';
input.classList.remove('error');
errorDiv.classList.add('hidden');
translations[key] = value;
}
if (value === '') {
itemDiv.dataset.status = 'pending';
} else if (!isValid) {
itemDiv.dataset.status = 'error';
} else {
itemDiv.dataset.status = 'completed';
}
}
function updateStats() {
const totalItems = Object.keys(englishData).filter(key =>
!key.startsWith('@') && key !== '@@locale' && typeof englishData[key] === 'string'
).length;
const completed = Object.keys(translations).length;
const remaining = totalItems - completed;
const percentage = totalItems > 0 ? Math.round((completed / totalItems) * 100) : 0;
// Count new vs existing items
const allItems = document.querySelectorAll('.translation-item');
const newItemsCount = document.querySelectorAll('.translation-item[data-type="new"]').length;
const existingItemsCount = document.querySelectorAll('.translation-item[data-type="existing"]').length;
document.getElementById('totalItems').textContent = totalItems;
document.getElementById('completedItems').textContent = completed;
document.getElementById('remainingItems').textContent = remaining;
document.getElementById('newItems').textContent = newItemsCount;
document.getElementById('existingItems').textContent = existingItemsCount;
document.getElementById('progressFill').style.width = percentage + '%';
document.getElementById('progressText').textContent = percentage + '% Complete';
const exportBtn = document.getElementById('exportBtn');
const targetLanguage = document.getElementById('targetLanguage').value.trim();
if (completed === totalItems && completed > 0 && targetLanguage) {
exportBtn.disabled = false;
} else {
exportBtn.disabled = true;
}
}
function updateTargetLanguage() {
const value = document.getElementById('targetLanguage').value.trim();
targetLocale = value;
document.getElementById('targetLocaleDisplay').textContent = value || '-';
updateStats();
}
function applyFilter() {
const items = document.querySelectorAll('.translation-item');
items.forEach(item => {
const type = item.dataset.type;
const status = item.dataset.status || (item.querySelector('.translation-input').value.trim() ? 'completed' : 'pending');
let show = false;
switch (currentFilter) {
case 'all':
show = true;
break;
case 'new':
show = type === 'new';
break;
case 'existing':
show = type === 'existing';
break;
case 'pending':
show = status === 'pending' || status === 'error';
break;
case 'completed':
show = status === 'completed';
break;
}
item.style.display = show ? 'block' : 'none';
});
}
function exportARB() {
const targetLanguage = document.getElementById('targetLanguage').value.trim();
if (!targetLanguage) {
alert('Please enter a target language code.');
return;
}
const newARB = {
'@@locale': targetLanguage
};
for (const [key, translation] of Object.entries(translations)) {
newARB[key] = translation;
const metaKey = `@${key}`;
if (englishData[metaKey]) {
newARB[metaKey] = englishData[metaKey];
}
}
const content = JSON.stringify(newARB, null, 2);
const blob = new Blob([content], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `app_${targetLanguage}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
alert(`Translation exported successfully as app_${targetLanguage}.json!`);
}