-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathgenerate-translation-manifest.js
More file actions
135 lines (108 loc) · 4.3 KB
/
generate-translation-manifest.js
File metadata and controls
135 lines (108 loc) · 4.3 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
#!/usr/bin/env node
/**
* Generate translation manifest by scanning all elements for translation files
* This creates a registry of which components support which languages to prevent 404s
*/
const fs = require('fs');
const path = require('path');
function generateTranslationManifest() {
const manifest = {};
const elementsDir = path.join(__dirname, '..', 'elements');
console.log('🔍 Scanning for translation files...');
if (!fs.existsSync(elementsDir)) {
console.error('❌ Elements directory not found:', elementsDir);
return;
}
const elements = fs.readdirSync(elementsDir);
let totalFiles = 0;
let elementsWithTranslations = 0;
const languageSet = new Set();
elements.forEach(elementDir => {
const elementPath = path.join(elementsDir, elementDir);
// Skip if not a directory
if (!fs.statSync(elementPath).isDirectory()) {
return;
}
const localesPath = path.join(elementPath, 'locales');
if (fs.existsSync(localesPath)) {
const files = fs.readdirSync(localesPath);
const translationFiles = files.filter(f => f.endsWith('.json'));
if (translationFiles.length > 0) {
elementsWithTranslations++;
console.log(` 📁 ${elementDir}:`);
// Group files by namespace
const namespaces = {};
translationFiles.forEach(file => {
totalFiles++;
// Parse filename: namespace.language.json
const parts = file.split('.');
if (parts.length >= 3) {
const language = parts[parts.length - 2]; // second to last part
const namespace = parts.slice(0, -2).join('.'); // everything before language and .json
if (!namespaces[namespace]) {
namespaces[namespace] = [];
}
namespaces[namespace].push(language);
languageSet.add(language);
console.log(` 📄 ${file} → ${namespace}:${language}`);
}
});
// Add to manifest
Object.keys(namespaces).forEach(namespace => {
manifest[namespace] = [...new Set(namespaces[namespace])].sort();
});
}
}
});
// Generate the manifest files
const manifestDir = path.join(__dirname, '..', 'elements', 'i18n-manager', 'lib');
const manifestPath = path.join(manifestDir, 'translation-manifest.json');
// Ensure directory exists
if (!fs.existsSync(manifestDir)) {
fs.mkdirSync(manifestDir, { recursive: true });
}
const sortedLanguages = Array.from(languageSet).sort();
// Create the JSON manifest
// NOTE: Intentionally omit timestamps to keep this file deterministic between builds
// and avoid needless commits during local testing.
const jsonManifest = {
_meta: {
elementsScanned: elements.length,
elementsWithTranslations: elementsWithTranslations,
translationFilesFound: totalFiles,
languagesSupported: sortedLanguages
},
manifest: manifest
};
// Write the JSON file
fs.writeFileSync(manifestPath, JSON.stringify(jsonManifest, null, 2));
console.log('\n✅ Translation manifest JSON generated successfully!');
console.log(` 📍 Location: ${manifestPath}`);
console.log(` 📊 Found ${Object.keys(manifest).length} namespaces with translations`);
console.log(` 🌍 Languages found: ${sortedLanguages.join(', ')}`);
// Show coverage stats
console.log('\n📈 Translation Coverage:');
Object.keys(manifest).forEach(namespace => {
console.log(` ${namespace}: ${manifest[namespace].length} languages [${manifest[namespace].join(', ')}]`);
});
// Show language popularity
const languageCounts = {};
Object.values(manifest).forEach(languages => {
languages.forEach(lang => {
languageCounts[lang] = (languageCounts[lang] || 0) + 1;
});
});
const popularLanguages = Object.entries(languageCounts)
.sort(([,a], [,b]) => b - a)
.slice(0, 10);
console.log('\n🏆 Most supported languages:');
popularLanguages.forEach(([lang, count]) => {
const percentage = ((count / Object.keys(manifest).length) * 100).toFixed(1);
console.log(` ${lang}: ${count} components (${percentage}%)`);
});
}
// Run the generator
if (require.main === module) {
generateTranslationManifest();
}
module.exports = { generateTranslationManifest };