-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-translations.js
More file actions
executable file
·312 lines (273 loc) · 8.42 KB
/
validate-translations.js
File metadata and controls
executable file
·312 lines (273 loc) · 8.42 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
#!/usr/bin/env node
/**
* Translation validation script for CI
*
* Validates that all translation files:
* - Have the same keys as en.json (reference)
* - Don't have missing translations
* - Have consistent placeholders
* - Don't contain untranslated English text
*
* Exit codes:
* - 0: All validations passed
* - 1: Validation errors found
*/
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const MESSAGES_DIR = join(__dirname, '..', 'messages');
const REFERENCE_LANG = 'en';
// Common English words that shouldn't appear in non-English translations
const ENGLISH_INDICATORS = [
'the',
'and',
'for',
'with',
'from',
'to',
'in',
'on',
'at',
'click',
'select',
'choose',
'save',
'cancel',
'close',
'open',
'settings',
'language',
'model',
'analysis',
'detection',
];
// Brand names and technical terms that are allowed to be identical
const ALLOWED_IDENTICAL = [
'Birda GUI',
'birda CLI',
'eBird',
'CUDA',
'TensorRT',
'GPU',
'CPU',
'WAV',
'MP3',
'FLAC',
'OGG',
'M4A',
'NSIS',
'JSON',
'API',
];
// Message keys that are allowed to be identical across languages
// (technical terms, abbreviations, proper nouns, etc.)
const IGNORED_IDENTICAL_KEYS = new Set([
// Audio/technical terms
'sourceFiles_mono',
'sourceFiles_stereo',
'table_columnClip',
'audio_pause',
// Short generic words
'settings_tab_data',
'sourceFiles_columnName',
'sourceFiles_columnFormat',
'sourceFiles_columnStatus',
'table_columnOffset',
// Size indicators
'settings_spectrogram_heightMedium',
'settings_spectrogram_heightXL',
// Calendar (month names are often similar across European languages)
'calendar_month_april',
'calendar_month_august',
'calendar_month_september',
'calendar_month_november',
// Weekday abbreviations (often similar)
'calendar_weekday_mo',
'calendar_weekday_fr',
'calendar_weekday_sa',
'calendar_weekday_su',
// Technical hints/labels
'species_fetch_weekHint',
'speciesSearch_detCount',
'analysis_statusCoords',
'settings_general_title',
// Sorting/technical labels
'species_card_maxConfidence',
'species_card_sortName',
]);
/**
* Load translations from a JSON file
* @param {string} lang - Language code
* @returns {Record<string, string>}
*/
function loadTranslations(lang) {
const filePath = join(MESSAGES_DIR, `${lang}.json`);
try {
const content = readFileSync(filePath, 'utf-8');
return JSON.parse(content);
} catch (error) {
throw new Error(`Failed to load ${lang}.json: ${error.message}`);
}
}
/**
* Extract placeholders from a translation string
* @param {string} text - Translation text
* @returns {string[]}
*/
function extractPlaceholders(text) {
const matches = text.match(/\{[^}]+\}/g);
return matches || [];
}
/**
* Check if text contains English words
* @param {string} text - Text to check
* @param {number} threshold - Minimum number of English words to detect
* @returns {boolean}
*/
function hasEnglishWords(text, threshold = 3) {
// Skip if text contains placeholders or special characters
if (text.includes('{') || text.includes('<') || text.includes('>')) {
return false;
}
const lowerText = text.toLowerCase();
let englishWordCount = 0;
for (const word of ENGLISH_INDICATORS) {
// Use word boundaries to avoid false positives
const regex = new RegExp(`\\b${word}\\b`, 'i');
if (regex.test(lowerText)) {
englishWordCount++;
if (englishWordCount >= threshold) {
return true;
}
}
}
return false;
}
/**
* Validate a language file against the reference
* @param {string} lang - Language code
* @param {Record<string, string>} reference - Reference translations
* @returns {{file: string, errors: string[], warnings: string[]}}
*/
function validateLanguage(lang, reference) {
const result = {
file: `${lang}.json`,
errors: [],
warnings: [],
};
const translations = loadTranslations(lang);
const referenceKeys = Object.keys(reference);
const translationKeys = Object.keys(translations);
// Check key count
if (referenceKeys.length !== translationKeys.length) {
result.errors.push(`Key count mismatch: expected ${referenceKeys.length}, got ${translationKeys.length}`);
}
// Check for missing keys
const missingKeys = referenceKeys.filter((key) => !(key in translations));
if (missingKeys.length > 0) {
result.errors.push(`Missing keys: ${missingKeys.join(', ')}`);
}
// Check for extra keys
const extraKeys = translationKeys.filter((key) => !(key in reference));
if (extraKeys.length > 0) {
result.errors.push(`Extra keys not in reference: ${extraKeys.join(', ')}`);
}
// Check each key
for (const key of referenceKeys) {
if (!(key in translations)) {
continue; // Already reported as missing
}
const refValue = reference[key];
const transValue = translations[key];
// Check for empty translations
if (!transValue || transValue.trim() === '') {
result.errors.push(`Empty translation for key: ${key}`);
continue;
}
// Check for identical values (possible untranslated)
if (lang !== REFERENCE_LANG && refValue === transValue) {
// Skip if key is in ignore list or contains allowed identical terms
if (!IGNORED_IDENTICAL_KEYS.has(key) && !ALLOWED_IDENTICAL.some((term) => refValue.includes(term))) {
result.warnings.push(`Possibly untranslated (identical to English): ${key}`);
}
}
// Check placeholder consistency
const refPlaceholders = extractPlaceholders(refValue).sort();
const transPlaceholders = extractPlaceholders(transValue).sort();
if (JSON.stringify(refPlaceholders) !== JSON.stringify(transPlaceholders)) {
result.errors.push(
`Placeholder mismatch in "${key}": ` +
`expected [${refPlaceholders.join(', ')}], got [${transPlaceholders.join(', ')}]`,
);
}
// Check for English words in non-English translations (skip brand names)
if (lang !== REFERENCE_LANG) {
const hasBrandNames = ALLOWED_IDENTICAL.some((term) => transValue.includes(term));
if (!hasBrandNames && hasEnglishWords(transValue)) {
result.warnings.push(`Possible English text in "${key}": "${transValue}"`);
}
}
}
return result;
}
function main() {
console.log('🔍 Validating translation files...\n');
try {
// Load reference language
const reference = loadTranslations(REFERENCE_LANG);
console.log(`✓ Reference (${REFERENCE_LANG}.json): ${Object.keys(reference).length} keys\n`);
// Get all language files
const files = readdirSync(MESSAGES_DIR).filter((f) => f.endsWith('.json') && f !== `${REFERENCE_LANG}.json`);
if (files.length === 0) {
console.log('⚠️ No translation files found besides reference\n');
process.exit(0);
}
const results = [];
let totalErrors = 0;
let totalWarnings = 0;
// Validate each language
for (const file of files) {
const lang = file.replace('.json', '');
const result = validateLanguage(lang, reference);
results.push(result);
totalErrors += result.errors.length;
totalWarnings += result.warnings.length;
}
// Print results
for (const result of results) {
const status = result.errors.length === 0 ? '✓' : '✗';
console.log(`${status} ${result.file}`);
if (result.errors.length > 0) {
console.log(` Errors (${result.errors.length}):`);
result.errors.forEach((err) => console.log(` • ${err}`));
}
if (result.warnings.length > 0) {
console.log(` Warnings (${result.warnings.length}):`);
result.warnings.forEach((warn) => console.log(` ⚠ ${warn}`));
}
console.log();
}
// Summary
console.log('─'.repeat(60));
if (totalErrors === 0 && totalWarnings === 0) {
console.log('✅ All translation files are valid!');
process.exit(0);
} else {
console.log(`Summary: ${totalErrors} error(s), ${totalWarnings} warning(s)`);
if (totalErrors > 0) {
console.log('❌ Validation failed');
process.exit(1);
} else {
console.log('⚠️ Validation passed with warnings');
process.exit(0);
}
}
} catch (error) {
console.error('💥 Validation script failed:', error.message);
process.exit(1);
}
}
main();